Can anyone explain, how this cat array is getting the id and name?
foreach ($all_categories as $menu) { ?>
<input type="checkbox" id="cat_<?php echo $menu->id; ?>" value="<?php echo $menu->id.'_'.$menu->name; ?>" name="cat[]">
..
..
}
and i can get the id and name in controller,by using like below. It is working totally fine. My question is, how id went to cat array in position 0,and name position 1?
$res_arr=explode('_',$value);
$cat_id=$res_arr[0];
$cat_name=$res_arr[1];
The explode function returns an array either it finds the delimiter or not. You gave a string to explode and asked it to split the string wherever it sees '_'. So in your case there was only one '_' and explode sliced the string into two pieces and placed them in an array respectively at [0] and [1].
Edit:
<?php
foreach ($all_categories as $menu) {
print "<input type=\"checkbox\" id=\"cat_".$menu->id."\" value=\"".$menu->id."_".$menu->name."\" name=\"cat[]\"/>";
}
?>
php short tag is not recommended Link
Related
This question already has answers here:
Passing an array using an HTML form hidden element
(8 answers)
Closed 3 years ago.
Hi so for the website I am designing I have been asked to use mal's eCommerce for the payment gateway. I have been trying to set up the remote call function they have so that once a purchase has been made it will make a call back to my website to run a script and sen email confirmation of their order.
This however has proved extremely frustrating as the session variables I have stored do not get passed through with the remote call, only certain values that they allow.
I tried storing them all in an array and putting them in one of the values I can change called sd:
$cart =array($_SESSION["date"], $_SESSION["name"], $_SESSION["email"], $_SESSION["number"], $_SESSION["address"], $_SESSION["town"], $_SESSION["postcode"],
$_SESSION["county"], $_SESSION["cake_type"], $_SESSION["collection"], $_SESSION["icingColour"], $_SESSION["trimColour"], $_SESSION["filling"], $_SESSION["wording"],
$_SESSION["cakePhoto"], $_SESSION["price"], $_SESSION["photo"] );
<input type="hidden" name="sd" value="<?php echo $cart?>">
Then in my script I tried:
$result = $_POST['sd'];
echo $result[0];
to test it
but the remote call just passed the value as "Array" so echo $result[0] just returned "A", the values was just passed as Array and none of the values in the array got passed with it.
So now I'm trying to store all my session variables in the sd value like this:
<input type="hidden" name="sd" value="<?php foreach ($_SESSION as $key=>$val) { echo $val;}?>">
and then I tested it by doing on my script:
$result = $_POST['sd'];
echo $result;
Now this does pass all the values but obviously just as one big value, is there a way I can split them up? Is there a better way with the array I haven't thought of? Any advice would be greatly appreciated because I'm at a complete loss
Edit:
Forgot to add this part:
So after the array has been split I would need to stored the values separately so something like:
$name = first value
$email = second value etc
try changing <input type="hidden" name="sd" value="<?php echo $cart?> to
<input type="hidden" name="sd[]" value="<?php echo $cart?>
or use serialize() then unserialize() the result.
Refer to Passing an array using an HTML form hidden element
You can use php implode and explode function.
The first part is implode.
We use ; as implode delimeter which joins array elements as single string using that delimeter.
<input type="hidden" name="sd" value="<?php foreach ($_SESSION as $key=>$val) { echo $val.';';}?>">
Explode converts that string to array again using the delimeter we passed.
foreach (explode(';', $_REQUEST["sd"]) as $key) {
echo $key;
echo "<br>";
}
Update : Try This
<input type="hidden" name="sd" value="<?php foreach ($_SESSION as $key=>$val) { echo $key.';'.$val.';;';}?>">
Explode converts that string to array again using the delimeter we passed.
foreach (explode(';;', $_REQUEST["sd"]) as $key) {
echo explode(';', $key)[0];
echo " = " ;
echo explode(';', $key)[1];
echo "<br>";
}
I have specific array that I cant find way to distinguish if it has multiple entries or single entry because when it has multiple entries I get
And when I get single entry I dont get [0] as first entry instead I get this
Im using foreach loop in php to extract data from array, but when it returns single item in array, it loops 3 times for each item in array :categoryid, name and other instead once and then, of course I get error
Warning: Illegal string offset 'CategoryID'
Warning: Illegal string offset 'Name'
How can I check if its single item or multiple items?
Code :
foreach($obj["Store"]["CustomCategories"]["CustomCategory"]as $category=>$val)
{
echo "<a href=\"#\" onclick=getCategory(\"";
echo $obj["Store"]["Name"];
echo "\",";
echo $val["CategoryID"];
echo ",1";
echo ");>";
echo $val["Name"];
echo "</a>";
}
You can test whether CustomCategory contains is an indexed or associative array by checking for an element with index 0. If not, you can wrap the contents in an array and then do your foreach loop.
$customCategory = $obj["Store"]["CustomCategories"]["CustomCategory"];
if (!$customCategory[0]) {
$customCategory = array($customCategory);
}
foreach ($customCategory as $category => $val) {
...
}
I have an html form where I am grouping inputs using name="array[]" then just looping through the array with PHP when submitted. Well I am using array[] to store the question, but when the question (array index) is longer than 64 characters then It will not pass that array key to my PHP.
HTML
<textarea name="corporate[CAN YOU SHOW US SIMILAR PROJECTS WITH THE SAME TARGET AUDIENCE? COMPETITORS?]"></textarea>
When I do:
var_dump($_POST['array']);
I get array(0)
But when I use a shorter index, it works.
Now if I manually create an associative array it works fine:
$array = array("CAN YOU SHOW US SIMILAR PROJECTS WITH THE SAME TARGET AUDIENCE? COMPETITORS?"=>"0");
What am I doing wrong?
I think it has to be a problem with going from the html form to the PHP. I am trying to loop through the inputs with my PHP so that I can loop through and display each question and corresponding answer with:
foreach ($array as $key=>$value) {
if ($value != NULL) {
echo '<strong>' . $key . '</strong><br/>';
echo $value . '<br/><br/>';
}
}
Which gives me:
Question
Answer
Question
Answer
etc.
How else could I do this without giving each input their own name to pass the question, or hard coding the question in my HTML?
It might be because you're doing this
var_dump($_POST['array']);
when you named the textarea "corporate". Try just doing
var_dump($_POST);
You can use a hidden input with the question and match up the indexes with the answer:
<input type="hidden" name="question[1]" value="CAN YOU SHOW US BLAH?">
<textarea name="answer[1]"></textarea>
I assume you are dynamically adding the question text? If so you should probably use htmlentities on it to avoid issues.
Then just loop one and access the other:
foreach ($_POST['answer'] as $key => $value) {
if (!empty($value)) {
echo '<strong>' . $_POST['question'][$key] . '</strong><br/>';
echo $value . '<br/><br/>';
}
}
I have this value from an XML file
<InfoExtractor:views>35490904</InfoExtractor:views>
From - http://www.infoextractor.org/upfiles/songlinks.txt.xml
it is not showing when I use this code
foreach ($viewfile->channel->item as $viewfileinfo):
$title=$viewfileinfo->title;
$views=$viewfileinfo->InfoExtractor:views;
echo "<span> ",$title,"</span> <br/> <span> ",$views,"</span> <br/>";
The title is showing fine when I echo, but how do I output the views value to echo?
==========
I found the answer, I changed the line to
$views=$viewfileinfo->children('InfoExtractor', true)->views;
This takes the colon into account.
You could use get_object_vars to capture an array, then view all the keys and values to determine precisely what you want to echo:
$views_array = get_object_vars($views);
foreach ($views_array as $key => $value) {
echo 'key = '.$key.', value = '.$value.'<br/>';
}
I have a PHP file that sends data to a 'ul' html element via ajax.
foreach ($data as $value) {
echo '<li id='. $value .'>'.$value.'</li>';
}
$data is a php array of the results received from SQL. So data will often be an array of 10 different words.
It works fine when the $value is one word.For example lets say $value = word1 (only one word, no spaces.)
<li id='word1'>word1</li> //this is correct
However, once in a while data while have an element that is two words. If $value is two words (word1 word2)(fish sticks), the 'li id' becomes:
<li id='fish' sticks> fish sticks</li> //this is wrong
Notice how in the 'id' word2(sauce) is not inside the quotes.
Any help is much appreciated.
It's because you aren't quoting your HTML attributes. Also, spaces in id attributes are not valid. You'll need to normalise the value for use in the attribute. Try something like this
function normaliseIdAttribute($id) {
return htmlspecialchars(strtolower(preg_replace(
'/\W+/', '-', $id)));
}
foreach ($data as $value) {
printf('<li id="%s">%s</li>',
normaliseIdAttribute($value),
htmlspecialchars($value));
}
you need use " to quote your id.
echo '<li id="'.$value.'">'.$value.'</li>'