1

I've made a script for movies but i have an issue. The home of script is contain code like this :

<div class="box">
<div id="movie"></div><div id="movie"></div><div id="movie"></div>
</div>

I've do a mysql query but i don't know how to echo just 3 record per div . Im using this code for mysql query :

$query=mysql_query("select * from movies where id=$id");
while($row=mysql_fetch_assoc($query))
  {
  echo $row['name'];
  }
1
  • I might be totally crazy but I'm wondering why this query would ever return more than one record. Commented Sep 6, 2013 at 23:33

2 Answers 2

1

You will need something like this I think

$query = mysql_query("select * from movies");

$result = array();

while($r = mysql_fetch_assoc($query)) {
    $result[$r['id']] = array($r['name'],$r['thumb']);
}

$i = 0;

foreach($result as $id => $data){

    if($i == 0)
    {
        echo "<div class=\"box\">";
        echo "<div id=\"movie\">";
        echo "ID: $id";
        echo "Name: $data[0]";
        echo "Thumb: $data[1]";
        echo "</div>";
        $i = $i + 1;
    }
    elseif($i == 1)
    {
        echo "<div id=\"movie\">";
        echo "ID: $id";
        echo "Name: $data[0]";
        echo "Thumb: $data[1]";
        echo "</div>";
        $i = $i + 1;
    }

    elseif($i == 2)
    {
        echo "<div id=\"movie\">";
        echo "ID: $id";
        echo "Name: $data[0]";
        echo "Thumb: $data[1]";
        echo "</div>";
        echo "</div>";
        $i = 0;
    }

}
Sign up to request clarification or add additional context in comments.

14 Comments

I've made some edits, but sure. As I understand it you want to show the movies three by three. This accomplishes this by firstly selecting every movie in your database with the $query, and then converting that result into an array. We then go by each row in the array (just as each row in the database) and process it accordingly. To make it go three-by-three we make the if() conditions. $i starts off as 0 so the upper if() conditions is going to be the first to be true. There we start the box and add the first movie and add 1 to the $i so that the next/middle if() statement is going tobe true
In the middle if() statement we just add another movie div, and again add 1 to the $i. Then lastly we add our last movie div and finish the box div. And reset the $i to start all over.
Sorry, I did some edits and have tested it. It works fine now in my environment. Let me know.
Woked As sharm now ! Thank you :3
No problem. The pleasure was all mine :) PHP doesn't natively have a nice way to return an array from a multi-row query.
|
1

Multiple elements with the same id? Not a good idea.

You didn't say what you want to happen if you're query returns less then 3 rows.

You might try:

...
$x=0;
while($row=mysql_fetch_assoc($query) && ++$x<=3) {
  echo $row['name'];
}

1 Comment

Yes it does - it doesn't necessarily give the result you're looking for - but I already pointed out that you didn't say what that was.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.