Page 2 of 2

Re: Help with mysql and php

Posted: Fri Aug 15, 2008 6:17 am
by JKM
So if I wan't it to be outputted like this:

Code: Select all

<div>
    <img src="<pic>" style="float:left;" />
    <p>Title: <title></p>
    <p>Director: <director></p>
    <p>Genre: <genre></p>
    <p>Rating: <rating></p>
    <p>Added: <added></p>
    <p>Review: <review></p> <!-- Only post this if review_yesno == 1 -->
    <p>IMDb-link: <imdb></p>
</div>
Should the php file look like this?

Code: Select all

while($row = mysql_fetch_row($result)) {
    echo "<div>\n";
    foreach($row as $key=>$value)     {
        if($key=='pic') {
            echo '<img src="'.$row['pic'].'" style="float:left;" />\n';
        }
        elseif($key=='title') {
            echo '<p>Title: '.$row['title'].'</p>\n';
        }
        elseif($key=='director') {
            echo '<p>Director: '.$row['director'].'</p>\n';
        }
        elseif($key=='genre') {
            echo '<p>Genre: '.$row['genre'].'</p>\n';
        }
        elseif($key=='rating') {
            echo '<p>Rating: '.$row['rating'].'</p>\n';
        }
        elseif($key=='Added') {
            echo '<p>Added: '.$row['added'].'</p>\n';
        }
        elseif($key=='imdb') {
            echo '<p>IMDb-link: <a href="http://imdb.com/'.$row['imdb'].'" target="_blank">IMDb</a></p>\n';
        }
        elseif($key=='review_yesno') {
            if($value=='1') {
                echo '<p>Review: '.$row['review'].'</p>\n';
            }
            elseif($value=='0') {
                echo '<!-- No review -->\n';
            }
        }
    }
    echo "</div>\n";
}

Re: Help with mysql and php

Posted: Sat Aug 16, 2008 10:05 am
by lukewilkins
That appears to be correct from just looking over it. Run and see ;-)

Re: Help with mysql and php

Posted: Sat Aug 16, 2008 1:24 pm
by JKM
Hmm, it doesn't get the values. And it's only the first if not the other elseifs, that gets posted. The source code turned out like this:

Code: Select all

<body>
<div>
<img src=".jpg" border="0" alt="" />
</div>
<div>
<img src=".jpg" border="0" alt="" />
</div>
 
</body>
 

Re: Help with mysql and php

Posted: Sun Aug 17, 2008 3:44 am
by desmi
The way you need to do it is not with else if's, your if statement only returns the first if that matches, or then else.

your code:

Code: Select all

 
 if($key=='pic') { // if $key == 'pic' it echose that pic, and exits that if statement
             echo '<img src="'.$row['pic'].'" style="float:left;" />\n';
         }
         elseif($key=='title') { // if $key == 'pic', this is not even tested..
             echo '<p>Title: '.$row['title'].'</p>\n';
         }
 
so you need to do it like this:

Code: Select all

 
if($x=='1') { echo $x; }
if($y=='1') { echo $y; }