while loop inside a while loop? Separating data from PHP - php

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;
}

Related

xPath, complicated foreach loop

I am using xPath to get a table , the table's first column has a link to another page, I want that link to link on the name column (first column). Any help would be appreciated.
Here is what I have:
echo '<table>';
foreach($xpath->query('//*[#id="CRConcernedPersonel1_DataGridCRPersonel"]/tr') as $row) {
echo '<tr>';
foreach($xpath->query('td[position() > 0]', $row) as $col) {
echo '<td>'.trim($col->textContent).'</td>';
foreach($xpath->query('a/#href', $col) as $link)
echo '<a href="'.trim($link->textContent).'"</a>'.'Link text'."\n";
}
echo '</tr>';
}
echo '</table>';
Here is the output:
http://pastebin.com/5PjCae74
Thanks!
It should work when you change
echo '<a href="'.trim($link->textContent).'"</a>'.'Link text'."\n";
to
echo 'Link text'."\n";
Currently your output reads e.g. like
<a href="CRDetails.aspx?PID=84539&Disp=1&inq=1"</a>Link text
therefore the link's not working. Changing the line as suggested should result in
Link text

PHP drop down from JSON populating, but text is invisible

I am attempting to create a drop down of items from a JSON file located on a remote server. The drop down appears to be populating (as there are options to choose from), but the text is not visible. I have attempted changing the style color (worth a try, right?) and multiple browsers.
<?php
echo '<select name="version" style="width: 300px">';
$url = 'http://s3.amazonaws.com/Minecraft.Download/versions/versions.json';
$jsonData = file_get_contents($url);
$jsonDataObject = json_decode($jsonData);
foreach($jsonDataObject->versions as $option){
echo '<option value=' . $option->type . '</option>';
}
echo '</select>';
?>
Thanks in advance for any assistance offered.
You are not populating the display text
echo "<option value= { $option->type } >{$option->type}</option>";
This is a little different from your statement, but the logic is the same. You need to write some text between option tags
<option value="val">displayText</option>
It should be something like-
echo '<option value=' . $option->type . '>'.$SOME_VALUE_HERE.'</option>';
// ^

DOM not parsing correctly

I have a script which loads items from an XML file, and displays them, so a user can choose which item they want to remove from their file. Here's the PHP:
<?php
global $current_user;
get_currentuserinfo();
$userid= $current_user->ID;
$dom = new DOMDocument;
$dom->load("playlists/$userid.xml");
echo '<div class="styled-select">';
echo '<center><form name="input" action="/remove/removesure.php" method="get">';
echo '<select name="q[]" size="2" multiple>';
$titles = $dom->getElementsByTagName('title');
foreach ($titles as $title) {
echo '<option>'.$title->nodeValue.'</option>';
}
echo '</select>';
echo '<input type="submit" class="submit" value="Remove">';
echo '</form></center>';
echo '</div>';
?>
The problem I've ran into is that it doesn't display some objects correctly, mainly items with hyphens (it displays – instead of -) and titles with spaces at the end, and because of this, my removal code doesn't find the item, and so can't remove it. I don't know what to do, and I don't know why it's doing this. I'm running the code in wordpress, if that makes a difference.
Any ideas?
If there is no chance of having any kind of councurrency, I would suggest you to use the title index of the title tag as value of "option". Eg:
$titles = $dom->getElementsByTagName('title');
$counter = 0;
foreach ($titles as $title) {
echo '<option value='.$counter.' >'.$title->nodeValue.'</option>';
$counter++;
}
In removesure.php, you could handle this by using an XPath expression like the next one:
//Title[2]
where "2" is the index of the title that must be removed.
That is a possible solution; another path you could try to follow is to handle spaces providing the best encoding option for your titles. htmlentity is the function that you should execute
echo '<option>'.htmlentity($title->nodeValue).'</option>'

i need fix select form on php code

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.

Having trouble with echoing HTML in php script

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.

Categories