I'm learning yii framework for an e-commerce project and it was going great so far. I have an addition form for estates and that form is generating using database. The code below, generates the form (/views/ad/_form), and giving names to input fields like detail["ad-title"], detail["ad-image"] etc.
<?php
$connection = Yii::app()->db;
$command = $connection->createCommand("SELECT * FROM eml_ozellikler");
$options = $command->queryAll();
$command = $connection->createCommand("SELECT * FROM eml_kurallar");
$rules = $command->queryAll();
$i = 0;
foreach($options as $option){
echo '<div class="row">';
echo $form->labelEx($model, 'detail["'.$option['name'].'"]');
switch($option['tur']){
case "textfield":
echo $form->textField($model, 'detail["'.$option['name'].'"]');
break;
case "textarea":
echo $form->textArea($model, 'detail["'.$option['name'].'"]', array('rows'=>'5','cols'=>'40'));
break;
case "integer":
echo $form->textField($model, 'detail["'.$option['name'].'"]');
break;
case "selectbox":
CHtml::dropDownList($option['label'], 'detail["'.$option['name'].'"]', $rules[$i]['values']);
break;
case "radio":
break;
case "file":
echo $form->fileField($model, 'detail["'.$option['name'].'"]');
break;
case "image":
break;
}
echo $form->error($model,'detail["'.$option['name'].'"]');
echo '</div>';
$i++;
}
?>
The problem is when giving rules to them. Rules are working just for looking, when i add to rules this
array('detay["ad-title"]', 'required'),
Then, that field is being required and getting a (*). But when i submit the form, then it gives an error saying "Ad.detail["ad-title"]" value isn't defined.
Without rules, i can post and get the posted value correctly using $_POST['Ad']['detail']["ad-title"] etc.
Also; i checked the Yii Framework docs but couldn't find any useful thing except tabular input and Form Builder, and i couldn't implement it to my code. Because i don't want to create variables at my model, i just want to send data using just one variable and rule.
Thanks, çağlar.
I did not find any rule regarding this as you are giving.
array('detay["ad-title"]', 'required'),
if ad-title is your table field name, then you may write.
array('ad-title', 'required'),
that's it ...
otherwise you need to learn rules, below is the one of the link where you can learn.
http://www.yiiframework.com/wiki/56/
Thanks.
Related
i am trying to show information using IF/ELSEIF commands
i have A through Z along the top of the page and want to show a table with all results starting with each letter
for example i have
<a href='?a'>A</a>
<?php
if($_GET == a)
{
echo "<table><tr><th>[picture]</th><th>information</th></tr>";
}
?>
i want to show a table with all the information, how would i do this using IF/ELSE commands? is there a better way of doing this without going to a different page?
thanks in advance for any help
I think it would be easier/cleaner to use a switch-case instead of if-else for your purpose here.
First off, try changing the links to something like this:
<a href='?l=a'>A</a>
and
<a href='?l=b'>B</a>
Then you should try to access the chosen letter with something like this:
<?php
$sLetter = null;
if (isset($_GET['l'])) {
$sLetter = strtolower($_GET['l']);
}
switch ($sLetter) {
case 'a':
echo "Information related to A";
break;
case 'b':
echo "Information related to B";
break;
// Continue in a similar way for the remaining letters
default:
echo "No information..."; // or perhaps show all A-Z information
break;
}
Note: For testing purposes, this is okay. But Superglobals should always be validated and sanitised to make your application more secure.
I'm trying to get a list of all my subscriptions in Woocommerce. I have read about WC_Subscriptions_Manager::get_subscription(), but I don't know how to use it. Please keep in mind that I have almost zero knowledge concerning PHP.
Someone asked a similar question on the forum. I finally used the following code:
<?php if(isset($_REQUEST['Action']))
{
$Action = $_REQUEST['Action'];
switch($Action)
{
case "ValidateSubscription":
include('../../wp-load.php'); //Guessing this path based on your code sample... should be wp root
$Subscriptions = WC_Subscriptions_Manager::get_all_users_subscriptions();
print_r($Subscriptions);
break;
default:
echo "invalid action";
}
}else
{
echo "no action specified";
}
?>
How can I use it?
Thanks for your help!
You can use wcs_get_users_subscriptions(). If you don't pass a user_id argument, the default is to get the current user. The function returns all subscriptions.
I am creating my first PHP script that tests the user on cranial nerve association; specifically the name of the cranial nerve is displayed to the user and they are supposed to click on the correct number corresponding to the name of the cranial nerve.
I will post my code first and my question after:
<?php
$generated_nerve_number = mt_rand(1,12);
switch($generated_nerve_number) {
case '1':
echo "Olfactory";
break;
case '2':
echo "Optic";
break;
case '3':
echo "Oculomotor";
break;
case '4':
echo "Trochlear";
break;
case '5':
echo "Trigeminal";
break;
case '6':
echo "Abducens";
break;
case '7':
echo "Facial";
break;
case '8':
echo "Vestibulocochlear";
break;
case '9':
echo "Glossopharyngeal";
break;
case '10':
echo "Vagus";
break;
case '11':
echo "Accessory";
break;
case '12':
echo "Hypoglossal";
break;
}
?>
<html>
<head>
<title>Cranial Nerves Test</title>
</head>
<body>
<p>Select the cranial nerve number below associated with the name of the cranial nerve given above:</p>
<form action="cranial.php" method="POST">
<?php
echo "Cranial Number: ";
for($i = 1; $i <= 12; $i++) {
echo "<input type=\"submit\" name=\"nerve_$i\" class=\"nerve_number\" value=\"$i\">";
}
?>
<?php
$submit = (isset($_POST['nerve_' . $i])) ? $_POST['nerve_' . $i] : NULL;
if($submit) {
$selected_nerve_number = $_POST['nerve_' . $i];
if($generated_nerve_number == $selected_nerve_number) {
echo "That is correct!";
} else {
echo "Sorry that is incorrect.";
}
}
?>
</form>
</body></html>
A quick overview of my thought process: I am randomly generating a number between 1-12 and using that number in a switch statement to echo the name of the cranial nerve. I used a for loop to generate submit buttons with names that contain the number of the cranial nerve corresponding to its displayed value. Lastly, my plan on checking whether the answer is correct or not is to use an if statement comparing the randomly generated number to the number selected and, if this is true, output a message saying that they were correct.
This is where the problem comes in: when I click any of the buttons, whether its the correct answer or not, the page just refreshes without giving any feedback on whether the answer was right or wrong. Can someone please point out the flaw?
Additionally, if there is a more optimal way of doing something in this script please let me know.
Thanks in advance.
There is a slight problem with this approach. Your script is actually having two steps to consider: first, sending a page to the client with a randomly chosen cranial nerve, then comparing a choice with what was randomly specified.
If you wish your script to do this, you must add some logic so that it will know it have to react differently.
if (isset($_POST))
{/*compare the user choice with what you had sent.
you will have to do add an input to your form containing the random value AND the chosen value, or you eon't be able to compare them*/}
currently, you're trying to do all this at once, eg, you're comparing the return value of your user before you even receive it in your post! Understand that once sent to the client (browser) your page is not linked anymore to the php. So here:
if($generated_nerve_number == $selected_nerve_number) {
you're actually comparing the random number you just generated, with the answer you will have from your user in the FUTURE, since he has not even seen it on screen yet :)
Basically I am using codeigniter and have LOTS of fields in each row for a database table.
(for this example I'll pretend its a blog entry with just title/postbody but thats only to keep it simple)
I know i could manually code in something like this:
(psedudo code)
if (!$submitted) {
$data = get_existing_data_from_db();
$showform=true;
}
if ($submitted) {
if (process_and_save_data_if_all_valid_data()) {
echo "done";
}
else {
$data = get_data_from_submitted_data();
$showform=true;
}
}
if ($showform) {
echo "<form>";
echo "<input type='text' name='title' value='{$data->title}' />";
}
but is there any CI class or library that would let me do something more along the lines of this (again, pseudo code)
$fields_to_edit = array('title','postbody');
$this->form_helper->edit_table_with_fields('posts', $fields_to_edit);
and the form_helper thing would automatically validate (obviously id set the rules) + show the form to the user, and save it (update mysql) if all correct
does something like this exist for code igintor?
This form builder may make things easier for you. http://formigniter.org/
I think what you're looking for is a "codeigniter CRUD generator", just Google this and you will find quite a few. Picking one is up to you. Example 1, Example 2...
I have:
one
And:
two
PHP page:
<?php
if mypage=one then
echo "my stuff text........one";
if mypage=two then
echo "my stuff text........two";
?>
I want to get text separately for each link from same php page
First of all, if then construct is not available in PHP so your code is syntactically wrong. The use of switch as suggested already is a good way to go. However, for your problem, you should use $_GET['mypage'] instead of $_POST['mypage']. It seems you are beginning PHP. Once you get some good basics, you will probably be making use of the functions such as include() and require(). Make sure you do not make mistakes beginners do:
<?php
if (isset($_GET['mypage'])
{
#include($_GET['mypage']);
}
?>
The above code works and looks simple but it has a very dangerous implementation allowing the malicious users to perform an attack known as file inclusion attack. So you should try to use the switch statements such as:
<?php
$mypage = $_GET['mypage']; //also you might want to cleanup $mypage variable
switch($mypage)
{
case "one":
#include("one.php");
break;
case "two":
#include("two.php");
break;
default:
#include("404.php");
}
?>
Umm, that php is not even remotely valid code. You want a switch statement:
<?php
$mypage = isset($_GET['mypage']) ? $_GET['mypage'] : '';
switch ($mypage) {
case 'one':
echo "my stuff text........one";
break;
case 'two':
echo "my stuff text........two";
break;
default:
header('HTTP/1.0 404 Not Found');
echo 'This page does not exist';
}