Hello I need to fix a problem on my PHP code. When I write the value name="" and id="" aren't found. Here's the code:
<?php
$tipos= $eachoption['option_value'];
$categorias='';
$cats = explode(",",$tipos);
echo "<select name=\"option_<?php echo $eachoption['option_id'];?>[]\" id=\"<?php echo $_POST['option_'.$eachoption['option_id']][$i];?>\">";
foreach($cats as $cat){
$cat = trim($cat);
$categorias .= "<option>". $cat ."</option>";
}
echo $categorias;
echo "</select>";
?>
Thanks! I think that maybe is for " or ' inside echo.
Your PHP syntax is incorrect. You cannot embed PHP-within-PHP. e.g.
<?php
$foo = "<?php echo 'bar' ?>";
will NOT execute that echo call. You are assigning the literal characters <, ?, p, etc... to a string.
Since you're using double-quoted strings, you don't need the echoes for simple variable insertions at all:
echo "<select name=\"option_{$eachoption['option_id']}[]\" id=\"" . $_POST['option_'.$eachoption['option_id']][$i]; . "\">";
^^^^^^^^^^^^^^^^^^^^^^^^^^
note that the second $_POST does require breaking out of string mode, since you're dynamically creating the array key.
Related
I have an array $category_slugs
how to output this using echo?
I have an error in code bellow:
echo "<div class='transition ".foreach($category_slugs as $slug){echo $slug;echo ' ';}."' data-category='transition'> " ?>
Thanks
You can't use foreach inside a echo. To achieve what you are trying you can use implode function, it concatenates the values of an array on a string.
echo "<div class='transition " .implode(' ', $category_slugs). "' data-category='transition'> " ?>
try this code
<div class="transition <?php foreach($category_slugs as $slug){echo $slug . ' ';} ?>" data-category="transition">
The concatenation operator ('.'), which returns the concatenation of its right and left arguments.
from php manual
But this code not a string.
foreach($category_slugs as $slug){echo $slug;echo ' ';}
So you will get a error.
This manual may help you Escaping from HTML.
Sorry for my bad english, I hope it will help you.
try this
$category_slugs =array('a','b','c');
foreach($category_slugs as $slug){
echo "<div class='transition ".$slug.' '."' data-category='transition'></div> " ;
}
Wrap <div> in a for loop, like so
<?php
$slug_string = "";
foreach($category_slugs as $slug){
$slug_string .= $slug_string." ";
}
echo "<div class='transition ".$slug_string."' data-category='transition'> ";
?>
I want to put the select clause in php html.
here my code:
<?php
for($index=1; $index<=$content->option->dynamic_field_index; $index++){
echo "<td><select name='kboard_option_product'
id='kboard_option_product{$index}' data-placeholder='selectProduct'>";
echo "<optgroup value='0' label='products'>";
echo "<option value='windows'>windows</option>";
echo "<option value='linux'>linux</option>";
echo "<option value='unix'>unix</option>";
echo "</optgroup></select></td>";
}?>
How can I fix it?
I want to do that in
If you look at the options below you will save the data like this, but if you use echo, you have to match php syntax and html syntax.
<option value="window"<?php if($content->option->product == 'window'):?> selected<?php endif?>>window</option>
What I want to do is to use the for statement in the php clause and use the tag with echo to show it as html
Just use the following ,
function php_selectbox(){
ob_start();
?>
//Place your html here and echo the function outside.
<?php
return ob_get_clean();
}
while($row = mysql_fetch_array($result))
//Template for each card in search result
{
echo '<div class="sleeve">';
echo '<div class="card ', $row['name'], '">';
echo '<div class="front face">';
echo '<img src="/', $row['cardset'], '/', $row['name'], $row['altart'], '.jpg"', ' alt="', $row['name'], '" />';
echo '</div>';
echo '<div class="back face">';
echo '<a id="name">', $row['name'], '</a><br/>';
echo '<form name="info" action="">';
echo '<select name="set">';
//while???
echo '<option value="Zendikar">', $row['sets'],'</option>';
echo '</select>';
echo 'Foil:<input type="checkbox" name="foil" value="true"/><br/>';
echo '<select name="condition">';
echo '<option value="Near Mint">Mint</option>';
echo '<option value="Played">Played</option>';
echo '<option value="Damaged">Damaged</option>';
echo '</select>';
echo 'Trade:<input type="checkbox" name="trade" value="true"/ <br/>';
echo '</form>';
echo '<b>Rulings:</b> <br/>';
echo $row['rulings'];
echo '</div>';
echo '</div>';
echo '</div>';
}
//while??? It might be hard to see in that mess of echoes, but there's a section that I have no idea how to deal with. PHP is grabbing rows of info, and populating them nicely. It fills the screen with little boxes of info.
In this section, that I don't know how to deal with, the data in one of the rows contains multiple things (thing1, thing2, thing3) separated by a (, ) each time. I need each of those things in a new thing1
So I feel like there would be another while loop inside each card?
You're on the right track with a loop. Try something like this, which explodes the string into an array, based on the comma delimiter, with explode():
echo '<select name="set">';
foreach( explode( ',', $row['sets']) as $item)
echo '<option>', $item, '</option>';
echo '</select>';
You probably need a foreach statement there after exploding the String into an array:
instead of the //while line and the following one:
foreach (explode(',', $row['sets']) as $value)
echo '<option value="', $value, '">', $value,'</option>';
I guess you may actually have another value for each row (one to be displayed, the other one is the actual value you want to set), but then the String would look much more like "(key1=value1, key2=value2)" and then you need a little more work, but you get the idea.
Hope this helps.
Yes, you would need to first explode that row into an array
$list_of_things = explode(",", $row['whatever']);
and then use a while, or a foreach:
$thing_options = '';
foreach($list_of_things as $thing)
$thing_options .= "<option>$thing</option>";
You might also find the here document syntax useful:
print <<<TEMPLATE
<div class="sleeve">
<div class="card {$row['name']}">
<div class="front face">
<img src="/{$row['cardset']}/{$row['name']}{$row['altart']}.jpg"
alt="{$row['name']}" />
</div>
<div class="back face">
<a id="name">{$row['name']}</a>
<br/>
<form name="info" action="">
<select name="set">
{$thing_options}
<option value="Zendikar">{$row['sets']}</option>
</select>
...
TEMPLATE;
While all of the answers telling you to explode() the array are correct, I can't help but think that having a db column filled with comma separated values are a symptom that your database is not normalized. You should check out the following link for an introduction to db normalization: http://mikehillyer.com/articles/an-introduction-to-database-normalization/
I'd also recommend not echoing out HTML. Ideally, your PHP scripts should follow this pattern:
All PHP processing up front, including database queries and form handling. Results should be stored in variables.
|
|
|
V
Almost pure HTML template/view, with just enough display logic (if/else, loops, echo) to actually display the results you stored in the variables from step 1.
You'll find that debugging/editing is a lot simpler if you let PHP be the brains and HTML be the beauty. Just like how markup and styles should be separate, the same goes for scripting and display.
You can use PHP's explode-method to split the comma separated string into its values, and then handle that array in a foreach loop:
$raw = "one,two,three";
$values = explode("," $raw);
foreach($values as $value) {
echo $value;
}
So... I have a mysql_fetch_array and I'm running into an issue when some of the mysql data contains single or double quotes. This is the dumbed down version of my code:
while($row=mysql_fetch_array($list)) {
echo "<tr>";
echo "<td onclick='edit_form(\"" . $row['item'] . "\");'>" . $row['item'];
echo "</td></tr>";
}
The edit_form() function is used to send the value of the current item back to the value of the input in the form so the user can then easily edit their entry and then sends an UPDATE command to mysql along with the proper primary key id (which I left out because of irrelevancy). The only issue I have is if a user puts single or double quotes into the form then it messes up the onclick attribute. Please help!! I am pretty new to php and can't figure this out. I've messed around with htmlentites() and html_entity_decode() but am still getting no where. Thank you so much!
Use htmlspecialchars on $row['item'] before inserting it in your document.
So your "dumbed-down" code should be:
while($row=mysql_fetch_array($list)) {
$item = htmlspecialchars($row['item']);
echo "<tr>";
echo "<td onclick='edit_form(\"" . $item . "\");'>" . $item;
echo "</td></tr>";
}
Try the below line instead of the original in your code and see if it works:
echo "<td onclick='edit_form(\"" . str_replace('"',""e;",$row['item']) . "\");\">" . $row['item'];
And where you would like to display the $item field, just replace it in reverse if you get "e; instead of ' " '. For example:
$qoute_free= str_replace('"e','"',$passed_value);
If you are using it in javascript, the function can be as below:
function edit_form(passed_value)
{
new_value=passed_value.replace(/"e;/g,'"');
}
I advise using json_encode. That way you don't have to worry about special cases htmlspecialchars might miss (such as newlines).
while($row=mysql_fetch_array($list)) {
echo "<tr>";
echo "<td onclick='edit_form(" . json_encode($row['item'], JSON_HEX_APOS) . ");'>" . $row['item'];
echo "</td></tr>";
}
I'm just putting the values back into my <form> (when they click on a <td>) and when they hit SUBMIT it directs the data to a different .php file that uses a MySQL UPDATE instead of an INSERT. I finally found some code that works!
$list is a mysql_query I ran at the top of the document
$item is the title of one of my columns in MySQL
while($row=mysql_fetch_array($list)){
$item = json_encode($row['item']);
$item = str_replace("'","'",$item);
echo "<td onclick='edit_form(" . $item . ");'>" . $row['item'] . "</td>";
}
Now it display's correctly in the <form> when <td> is clicked (by jQuery input.val($item)) and it shows up in the table correctly via $row['item']. I don't really understand exactly how it's working (how the encode is making it work) but I am glad it is. Crazyness!!!! Thanks for responding! and thanks for your effort!!
I am trying to make a form that show users name and I am having a little trouble with this part. The ' ' and " " marks dont quite work how they are supposed to. Im trying to echo the options in drop down menu and some how the $wholenames and the last " sign appear in the wrong part of the page. Could someone please tell me what is the correct way of doing this?
Thanks
echo' "<option>'; echo $wholenames; echo'</option>"';
Actually I had looked it wrong it is a little bit more complex. Below you can see the code. The whole dropdown menu does not appear. The wholenames integer appears, but the menu does not...
echo'
<label for="addusertogroup">Add user to an existing group:</label>
<select name="addusertogroup" id="addusertogroup">
'; if(mysql_num_rows($userresult))
{
while($row2 = mysql_fetch_assoc($userresult))
{
$wholename = array("$row2[f_name] $row2[s_name]");
foreach ($wholename as $wholenames) {
echo "<option>$wholenames</option>";
}
}
}
else {
echo "<option>No Names Present</option>";
}
To make it work, simply do this:
echo "<option>";
echo $wholenames;
echo "<option>";
or this:
echo "<option>$wholenames</option>";
or this:
echo '<option>'.$wholenames.'</option>';
All will work, just up to you which one you pick.
You shouldn't have " before <option> and after </option>
It should be something like
echo "<option>". echo $wholenames; echo "</option>";
Also, if $wholenames is an array, you'd better iterate over it:
foreach ($wholenames as $name){
echo "<option>". echo $name; echo "</option>";
}
Any text for options in a HTML SELECT box are written inside the tag. If you don't put your text between the <option> tags the browser will try to insert it to the select box's DOM.
So you could change your code to this:
echo '<option value="myvalue">"' . $wholenames . '"</option>';
Haven't actually tested this code.
Update if the Quotation mark was not meant to be in the output you would simply need to write:
echo '<option value="myvalue">' . $wholenames . '</option>';
In php you can use both " " and ' ' with strings.