Create a second row when retrieving info from a database

Hi All
I'm using the following code to retrieve then display images from a database base. It works great. If I set $limit to 3 I can get it to create a 3 cell row and display an image in each cell. My question is this - How can I modify this to create another 3 cell row so that I can set $limit to 6 or 9 or 12 etc. and it will create
$limit = 6      (2 rows of 3 cells)
$limit = 9      (3 rows of 3 cells)
$limit = 12    (4 rows of 3 cells)
etc.

Can be seen in action at www.stmichaelslurgan.org.uk/pp2.php

At the minute setting the limit to 3 is fine but I have hundreds of photos to add and at the minute the database isn't searchable (no info on the pictures) so displaying them a number at a time is the only option.

<?php
$user = "";
$pass = "";
$db = "";

    @mysql_connect("localhost", $user, $pass) or die("ERROR--CAN'T CONNECT TO SERVER");
    @mysql_select_db($db) or die("ERROR--CAN'T CONNECT TO DB");

    $limit          = 3;                
//    $query_count    = "SELECT count(*) FROM imageinfo";    

$query_count = "SELECT COUNT(*) AS mycount FROM imageinfo";
$r = mysql_query("SELECT COUNT(*) AS mycount FROM imageinfo");
$o = mysql_fetch_object($r);
$totalrows = $o->mycount;

    $result_count   = mysql_query($query_count);    
//    $totalrows      = mysql_num_rows($result_count);

    if(empty($page)){
        $page = 1;
    }
         

    $limitvalue = $page * $limit - ($limit);
    $query  = "SELECT * FROM imageinfo LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error());

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

    $bgcolor = "#E0E0E0"; // light gray

echo("<table>");
    echo("<tr bgcolor=".$bgcolor.">");    
    while($row = mysql_fetch_array($result)){
//        if ($bgcolor == "#E0E0E0"){
//            $bgcolor = "#FFFFFF";
//        }else{
//            $bgcolor = "#E0E0E0";
//        }



echo '<td><a href="'.$row['image_path'].$row['image_number'].'.jpg" target=_blank><img src="'.$row['thumbnail_path'].$row['image_number'].'.jpg" border=0></a><br>';

echo '<b>No. :</b>'.($row['image_number']);
echo '<br><b>Year:</b>'.($row['Year']);
echo '<br><b>Info:</b>'.($row['info']);
echo ("</td>");

 
    }
echo("</tr>");



    echo("</table width=\"740\">");

    if($page != 1){
        $pageprev = $page  - 1;
 
         
        echo("<a href=\"$PHP_SELF?page=$pageprev\" class=links>PREV".$limit."</a>&nbsp;");
    }else{
        echo("PREV".$limit."&nbsp;");
    }

    $numofpages = $totalrows / $limit;
     
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo($i."&nbsp;");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\" class=links>$i</a>&nbsp;");
        }
    }


    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo($i."&nbsp;");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\" class=\"links\">$i</a>&nbsp;");
        }
    }

    if(($totalrows - ($limit * $page)) > 0){
        $pagenext = $page  + 1;
         
        echo("<a href=\"$PHP_SELF?page=$pagenext\" class=\"links\">NEXT".$limit."</a>");
    }else{
        echo("NEXT".$limit);
    }
     
    mysql_free_result($result);


?>


Many thanks in advance

Michael

LVL 10
daleoranAsked:
Who is Participating?
 
Diablo84Connect With a Mentor Commented:
hmm apparently PHP doesn't like the empty function being fed a mysql result, news to me, but lets try this:

$i=0;
while ($i<$limit) {
 
 echo "<tr bgcolor=\"$bgcolor\">";
 for ($j=1;$j<=3;$j++) {
  $img_number = @mysql_result($result,$i,"image_number");
  if (!empty($img_number)) {
   echo "<td><a href=\"".mysql_result($result,$i,"image_path").$img_number.".jpg\" target=\"_blank\"><img src=\"".mysql_result($result,$i,"thumbnail_path").$img_number.".jpg\" border=0></a><br>";
   echo "<b>No. :</b>".$img_number;
   echo "<br><b>Year:</b>".mysql_result($result,$i,"Year");
   echo "<br><b>Info:</b>".mysql_result($result,$i,"info");
   echo "</td>";
  }
  else {
   echo "<td>&nbsp;</td>";
  }
  $i++;
 }
 echo "</tr>";
 
}
0
 
NavicertsCommented:
$limit = 6      (2 rows of 3 cells)
$limit = 9      (3 rows of 3 cells)
$limit = 12    (4 rows of 3 cells)



$limit['cells'] = 3;                                                            //Changes number of pictures per row/page
$limit['rows'] = ANY_NUMBER;                                         //Changes actual "limit" number
$limit['rows'] = $limit['rows'] / $limit['cells'];





.....


Maybe i misunderstood, but wouldnt this do it?    Had your coffie yet ;-)


-Navicerts
0
 
daleoranAuthor Commented:
Hi Navicerts,
fortunately I don't drink coffee :)
And I am a total novice when it comes PHP. Setting the $limit sets the number of images that can be displayed on a page. At the minute I have it set to 3 and the table set in such a way that it displays 1 image per cell across the page. If I change it to 4,5,6 etc it will display 4,5,6 etc images across the page. What I want is to set the $limit to multiples of 3 and have a new table row of three cells to display the images. I know what I want and I'm probably describing it woefully :(

Michael
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Diablo84Commented:
Heres a quick fix for it

replace:

######################
    echo("<tr bgcolor=".$bgcolor.">");    
    while($row = mysql_fetch_array($result)){
//        if ($bgcolor == "#E0E0E0"){
//            $bgcolor = "#FFFFFF";
//        }else{
//            $bgcolor = "#E0E0E0";
//        }



echo '<td><a href="'.$row['image_path'].$row['image_number'].'.jpg" target=_blank><img src="'.$row['thumbnail_path'].$row['image_number'].'.jpg" border=0></a><br>';

echo '<b>No. :</b>'.($row['image_number']);
echo '<br><b>Year:</b>'.($row['Year']);
echo '<br><b>Info:</b>'.($row['info']);
echo ("</td>");

 
    }
echo("</tr>");
######################

With:

######################
$i=0;
while ($i<$limit) {
 
 echo "<tr bgcolor=\"$bgcolor\">";
 for ($j=1;$j<=3;$j++) {
  if (!empty(mysql_result($result,$i,"image_number")) {
   echo '<td><a href="'.mysql_result($result,$i,"image_path").mysql_result($result,$i,"image_number").'.jpg" target=_blank><img src="'.mysql_result($result,$i,"thumbnail_path").mysql_result($result,$i,"image_number").'.jpg" border=0></a><br>';
   echo '<b>No. :</b>'.mysql_result($result,$i,"image_number");
   echo '<br><b>Year:</b>'.mysql_result($result,$i,"Year");
   echo '<br><b>Info:</b>'.mysql_result($result,$i,"info");
   echo "</td>";
  }
  else {
   echo "<td>&nbsp;</td>";
  }
  $i++;
 }
 echo "</tr>";
 
}
######################

Note: i wrote this down there in the comment box so subsequently have not tested it.

0
 
Diablo84Commented:
There is a better method of doing this but its a little more complicated and seeing as we are working with a fairly simple display here i figure its best to keep it that way so you don't have chunks of code in your script that don't make any sense to you. Provided the above works ok i can send you a snippet of code from a gallery script i wrote at the beginning of this year as an example if it would be useful to you.
0
 
daleoranAuthor Commented:
Hi Diablo
get the following error message

Parse error: parse error, expecting `T_VARIABLE' or `'$'' in /home/casper/public_html/pp2.php on line 78

Which is the first line of this

  if (!empty(mysql_result($result,$i,"image_number")) {
   echo '<td><a href="'.mysql_result($result,$i,"image_path").mysql_result($result,$i,"image_number").'.jpg" target=_blank><img src="'.mysql_result($result,$i,"thumbnail_path").mysql_result($result,$i,"image_number").'.jpg" border=0></a><br>';

Michael
0
 
daleoranAuthor Commented:
Also Diablo is there a ) missing from the line
  if (!empty(mysql_result($result,$i,"image_number")) {
=if (!empty(mysql_result($result,$i,"image_number"))) {


Michael
0
 
Diablo84Commented:
hmm, i think this proves that coding in a textarea is not the best of ideas :)

try:

$i=0;
while ($i<$limit) {
 
 echo "<tr bgcolor=\"$bgcolor\">";
 for ($j=1;$j<=3;$j++) {
  if (!empty(mysql_result($result,$i,"image_number"))) {
   echo "<td><a href=\"".mysql_result($result,$i,"image_path").mysql_result($result,$i,"image_number").".jpg\" target=\"_blank\"><img src=\"".mysql_result($result,$i,"thumbnail_path").mysql_result($result,$i,"image_number").".jpg\" border=0></a><br>";
   echo "<b>No. :</b>".mysql_result($result,$i,"image_number");
   echo "<br><b>Year:</b>".mysql_result($result,$i,"Year");
   echo "<br><b>Info:</b>".mysql_result($result,$i,"info");
   echo "</td>";
  }
  else {
   echo "<td>&nbsp;</td>";
  }
  $i++;
 }
 echo "</tr>";
 
}
0
 
daleoranAuthor Commented:
I'm at home at the moment. Will check it out when I get into work tomorrow morning.

Many thanks

Michael
0
 
daleoranAuthor Commented:
Hi Diablo
tried that but still get the following message

Parse error: parse error, expecting `T_VARIABLE' or `'$'' in /home/casper/public_html/pp2.php on line 78

Michael

0
 
daleoranAuthor Commented:
Diablo, what can I say. Brilliant. Thank you very much

Michael

0
 
Diablo84Commented:
no problem :)

if you wanted to have a look at the alternative (slightly more complicated) way of doing this contact me at the address in my profile and il send the code to you.

|)iablo
0
All Courses

From novice to tech pro — start learning today.