0

I've got in my database for example 7 columns with 5 rows (ID,title , date,text,img), I want to get title and img from the last three colums and i did this:

<?$queryT = "SELECT * FROM mainpage ORDER BY ID DESC LIMIT  1";
        $resultT = mysql_query($queryT) or die(mysql_error()."[".$queryT."]");
        $queryI = "SELECT * FROM mainpage ORDER BY ID DESC LIMIT  1";
        $resultI = mysql_query($queryI) or die(mysql_error()."[".$queryI."]");
    ?>

        <?while ($rowT = mysql_fetch_array($resultT, MYSQL_BOTH)){
            while ($rowI = mysql_fetch_array($resultI, MYSQL_BOTH)){
        ?>
        <ul class="bjqs">
          <li><img src="<?php echo $rowI["IMG"]; ?>" title="<?php echo $rowT["TITLE"]; ?>" class="siderNews"></li>
          <li><img src="<?php echo $rowI["IMG"]; ?>" title="<?php echo $rowT["TITLE"]; ?>" class="siderNews"></li>
          <li><img src="<?php echo $rowI["IMG"]; ?>" title="<?php echo $rowT["TITLE"]; ?>" class="siderNews"></li>
        </ul>

        <?
        }
        }
        ?>

but it's printing me , three times the information from the last column only. I use and print that information in a slider.

Avtontom_
  • 305
  • 2
  • 6
  • 19

3 Answers3

0

try this once

 <?php $queryT = "SELECT * FROM mainpage ORDER BY ID DESC LIMIT  0,3";
    $resultT = mysql_query($queryT) or die(mysql_error()."[".$queryT."]");

?>
    <ul class="bjqs">
  <?php while ($rowT = mysql_fetch_array($resultT, MYSQL_BOTH)){     
    ?>
  <li><img src="<?php echo $rowI["IMG"]; ?>" title="<?php echo $rowT["TITLE"]; ?>" class="siderNews"></li>
   <?

    }?>
    </ul>
sasi kanth
  • 2,637
  • 4
  • 17
  • 29
nickle
  • 4,636
  • 1
  • 13
  • 11
  • Do not use short tags. They are deprecated. http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use – Konsole Sep 04 '13 at 10:37
  • thank, but it's not working now doesn't print nothing. Also the the slider doesn't work properly – Avtontom_ Sep 04 '13 at 10:44
0

You have a while statement inside another while statement. Meaning as it iterates through one while, it will iterate through the other one, depending on how many results it gets.

<?PHP
$queryT = "SELECT * FROM mainpage ORDER BY ID DESC LIMIT 3";
$resultT = mysql_query($queryT) or die(mysql_error() . "[" . $queryT . "]");
?>

<?PHP
while ($rowT = mysql_fetch_array($resultT, MYSQL_BOTH)) {
    ?>
    <ul class="bjqs">
        <li><img src="<?PHP echo $rowT["IMG"]; ?>" title="<?PHP echo $rowT["TITLE"]; ?>" class="siderNews"></li>
    </ul>

    <?PHP
}
?>
Ben Fortune
  • 31,623
  • 10
  • 79
  • 80
0

Try this:

<?php
    $queryT = "SELECT * FROM mainpage ORDER BY ID DESC LIMIT  0,3";
    $resultT = mysql_query($queryT) or die(mysql_error()."[".$queryT."]");
?>

<ul class="bjqs">
   <?php while($rowT = mysql_fetch_array($resultT, MYSQL_BOTH)) { ?>
      <li><img src="<?php echo $rowT["IMG"]; ?>" title="<?php echo $rowT["TITLE"]; ?>"        class="siderNews"></li>
    <?php } ?>
</ul>
Dragan Okanovic
  • 7,509
  • 3
  • 32
  • 48
Pragati
  • 16
  • 7