Solved

Create a second row when retrieving info from a database

Posted on 2004-10-13
12
210 Views
Last Modified: 2010-07-27
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

0
Comment
Question by:daleoran
  • 6
  • 5
12 Comments
 
LVL 7

Expert Comment

by:Navicerts
ID: 12297685
$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
 
LVL 10

Author Comment

by:daleoran
ID: 12298053
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12299124
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 27

Expert Comment

by:Diablo84
ID: 12299179
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
 
LVL 10

Author Comment

by:daleoran
ID: 12299253
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
 
LVL 10

Author Comment

by:daleoran
ID: 12299292
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12299904
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
 
LVL 10

Author Comment

by:daleoran
ID: 12300200
I'm at home at the moment. Will check it out when I get into work tomorrow morning.

Many thanks

Michael
0
 
LVL 10

Author Comment

by:daleoran
ID: 12306246
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
 
LVL 27

Accepted Solution

by:
Diablo84 earned 300 total points
ID: 12306396
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
 
LVL 10

Author Comment

by:daleoran
ID: 12306609
Diablo, what can I say. Brilliant. Thank you very much

Michael

0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12306622
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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question