php - Select one row at a time from a query with multiple rows? -


i have query selects 3 rows; want display 1 row @ time, use data row, move next row, repeat. how accomplished? table has 4 items in it.

php:

<?php $server = "secret"; $user = "secret"; $pass = "secret"; $db = "secret";  $con=mysqli_connect($server,$user,$pass,$db);  if (mysqli_connect_errno($con))    {        echo "failed connect mysql: " . mysqli_connect_error();    }  $sql = 'select * news order id desc limit 3';  mysqli_select_db($con, $db); $query = mysqli_query($con, $sql); $number = 1; while ($result = mysqli_fetch_array($query)) {      $id = $result['id'];     $title = $result['title'];     $news_date = $result['news_date'];     $post = $result['post']; }  mysqli_close($con); ?>  

attempt display:

        <div name='title'><?php echo $title; ?></div> || <div name='news_date'><?php echo $news_date; ?></div>         <p>news <?php echo $number; ?></p>         <p><?php echo $post; ?></p>         <?php               $number++;         ?> 

output of attempt:

test  ||  7/14/13  news 1  testing see if work lalalalalla 

note: tried repeat attempt see if work, displayed same output second 1 duplicate except said news 2

desired output look:

newest title | newest date newest news. etc. insert big paragraph of news here etc etc.  2nd newest title | 2nd newest date 2nd newest news. etc. insert big paragraph of news here etc etc.  3rd newest title | 3rd newest date 3rd newest news. etc. insert big paragraph of news here etc etc. 

the problem overwriting variables each while() loop -

while ($result = mysqli_fetch_array($query)) {      $id = $result['id'];     $title = $result['title'];     $news_date = $result['news_date'];     $post = $result['post']; } 

so when try echo $id, $title, $news_date, , $post, last row data

you either need add html in while loop -

while ($result = mysqli_fetch_array($query)) {      $id = $result['id'];     $title = $result['title'];     $news_date = $result['news_date'];     $post = $result['post'];     ?>     <div name='title'><?php echo $title; ?></div> || <div name='news_date'><?php echo $news_date; ?></div>     <p>news <?php echo $number; ?></p>     <p><?php echo $post; ?></p>     <?php           $number++; } 

or save them array

$id = array(); $title = array(); $news_date = array(); $post = array();  while ($result = mysqli_fetch_array($query)) {      $id[] = $result['id'];     $title[] = $result['title'];     $news_date[] = $result['news_date'];     $post[] = $result['post']; } 

and access array in display

    <div name='title'><?php echo $title[$number-1]; ?></div> || <div name='news_date'><?php echo $news_date[$number-1]; ?></div>     <p>news <?php echo $number; ?></p>     <p><?php echo $post[$number-1]; ?></p>     <?php           $number++;     ?> 

i used [$number-1] assume $number starting @ 1, , arrays 0 based


edit here ways echo html. beneficial visit php documentation, of there. in these set $number 0, can change 1, , change each of $number->$number-1, , $number+1->$number

using loop - http://php.net/manual/en/control-structures.for.php

<?php for($number=0;$number<count($title);$number++){ ?>     <div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>     <p>news <?php echo $number+1; ?></p>     <p><?php echo $post[$number]; ?></p> <?php    } ?> 

using foreach loop - http://php.net/manual/en/control-structures.foreach.php

<?php foreach($title $number => $value{ ?>     <div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>     <p>news <?php echo $number+1; ?></p>     <p><?php echo $post[$number]; ?></p> <?php    } ?> 

using while loop - http://php.net/manual/en/control-structures.while.php

<?php $number = 0; while($number<count($title){ ?>     <div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>     <p>news <?php echo $number+1; ?></p>     <p><?php echo $post[$number]; ?></p> <?php        $number++; } ?> 

each of above done closing out of php, , writing html, , opening php again. stay in php, , echo html - http://php.net/manual/en/function.echo.php

<?php for($number=0;$number<count($title);$number++){     echo "<div name='title'>".$title[$number]."</div> || <div name='news_date'>".$news_date[$number]."</div>";     echo "<p>news ".($number+1)."</p>";     echo "<p>".$post[$number]."</p>";      }     ?> 

Comments