Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
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?

share|improve this question

5 Answers

up vote 3 down vote accepted

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>';
share|improve this answer
that was so easy! you nailed it, thanks!! – Mallanaga Sep 6 '12 at 16:09

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;
share|improve this answer
so this would replace all the echoes? – Mallanaga Sep 6 '12 at 16:13
Yes. Of course you cannot put any PHP code (except variables) inside a here-document, so you would need to calculate the options array beforehand, store it in a variable, and reference {$options} inside the here-document. – lserni Sep 6 '12 at 16:18
Ideally - that's what @kevinmajor1 was saying - you would do all your processing before any output. Then you might even load the HTML code from an external .html (or .php) file instead of a here-document. This makes debugging much easier, and in case of an error you can produce a "clean" error page or redirect (since you didn't echo anything else yet) instead of a first half-baked page and then an error message. – lserni Sep 6 '12 at 16:23

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;
}
share|improve this answer

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.

share|improve this answer

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.

share|improve this answer
1  
thank you for that! I'll look into heeding your advice. =) – Mallanaga Sep 6 '12 at 16:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.