drewg23
drewg23

Reputation: 401

Output multiple records php

Okay so I think I just set up my MySQL table poorly. I id, other stuff that doesnt matter, then, movielink1, movielink2, movielink3, movielink4, movielink5

I want a button to appear for each one, ONLY if the movielink# exists. Most of them just have only movielink1 filled.

I think im just looking at it the wrong way. Here's my code.

             <?php 
            $data2 = mysql_query("SELECT movielink1,movielink2,movielink3,movielink4,movielink5 FROM movies WHERE id=$id");
            if (mysql_num_rows($data2) > 0 ) {
            while($info2 = mysql_fetch_array($data2)) {
                Print '<a href="'.$info['movielink1'];.'?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="'.$info['title'];.'"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
                }
            } else {
                Print 'Error!'; 
            }
            ?>

Thanks for suggestions, I just added this, but its still not working correctly, nor is a great way to approach it.

        $data1 = mysql_query("SELECT movielink1 FROM movies WHERE id=$id");
        if (mysql_num_rows($data1) > 0 ) {
        while($info1 = mysql_fetch_array($data1)) {
            Print '<a href="'.$info['movielink1'].'?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="'.$info['title'].'"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
            }
        } else {
        }

        $data2 = mysql_query("SELECT movielink2 FROM movies WHERE id=$id");
        if (mysql_num_rows($data2) > 0 ) {
        while($info2 = mysql_fetch_array($data2)) {
            Print '<a href="'.$info['movielink2'].'?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="'.$info['title'].'"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
            }
        } else {
        }
        $data3 = mysql_query("SELECT movielink3 FROM movies WHERE id=$id");
        if (mysql_num_rows($data3) > 0 ) {
        while($info3 = mysql_fetch_array($data3)) {
            Print '<a href="'.$info['movielink3'].'?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="'.$info['title'].'"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
            }
        } else {
        }
        $data4 = mysql_query("SELECT movielink4 FROM movies WHERE id=$id");
        if (mysql_num_rows($data4) > 0 ) {
        while($info4 = mysql_fetch_array($data4)) {
            Print '<a href="'.$info['movielink4'].'?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="'.$info['title'].'"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
            }
        } else {
        }
        $data5 = mysql_query("SELECT movielink5 FROM movies WHERE id=$id");
        if (mysql_num_rows($data5) > 0 ) {
        while($info5 = mysql_fetch_array($data5)) {
            Print '<a href="'.$info['movielink5'].'?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="'.$info['title'].'"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
            }
        } else {
        }

Upvotes: 0

Views: 66

Answers (1)

3dgoo
3dgoo

Reputation: 15794

There are a number of little problems in your code. Such as you fetch the row from results and set it to $info2 but then you call $info.

Here is your code fixed up:

$sqlResults = mysql_query("SELECT * FROM movies WHERE id=$id");

if (mysql_num_rows($sqlResults) > 0 ) {
    while($sqlRow = mysql_fetch_array($sqlResults)) {
        if (!empty($sqlRow['movielink1'])) {
            print '<a href="' . $sqlRow['movielink1'] . '?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="' . $sqlRow['title'] . '"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
        }
        if (!empty($sqlRow['movielink2'])) {
            print '<a href="' . $sqlRow['movielink2'] . '?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="' . $sqlRow['title'] . '"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
        }
        if (!empty($sqlRow['movielink3'])) {
            print '<a href="' . $sqlRow['movielink3'] . '?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="' . $sqlRow['title'] . '"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
        }
        if (!empty($sqlRow['movielink4'])) {
            print '<a href="' . $sqlRow['movielink4'] . '?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="' . $sqlRow['title'] . '"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
        }
        if (!empty($sqlRow['movielink5'])) {
            print '<a href="' . $sqlRow['movielink5'] . '?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="' . $sqlRow['title'] . '"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
        }

    }
} 
else 
{
    print 'Error!'; 
}

Upvotes: 1

Related Questions