Help with mysql and php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: Help with mysql and php

Post 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";
}
User avatar
lukewilkins
Forum Commoner
Posts: 55
Joined: Tue Aug 12, 2008 2:42 pm

Re: Help with mysql and php

Post by lukewilkins »

That appears to be correct from just looking over it. Run and see ;-)
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: Help with mysql and php

Post 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>
 
desmi
Forum Commoner
Posts: 64
Joined: Sun Jun 15, 2008 4:55 am

Re: Help with mysql and php

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