logo
Apache Lounge
Webmasters

 


About

Forum Index Downloads Search Register Log in  RSS Apache Lounge
 



Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Apache Lounge is not sponsored.

Your donations will help to keep this site alive and well, and continuing building binaries.



For Loop Problem

 
Post new topic   Reply to topic    Apache Forum Index -> Coding & Scripting Corner



View previous topic :: View next topic  
Author Message
peacemaker



Joined: 23 May 2008
Posts: 80

PostPosted: Mon 29 Jun '09 7:43    Post subject: For Loop Problem Reply with quote

Hi all, i am having problem regarding displaying images in tabular format.
Below is my code
Code:

$sql = "select * from upload ";
               
                $result = mysqli_query($link,$sql) or die ("Could not access DB: " . mysqli_error());
            
            $imgcnt = mysqli_num_rows($result);
            
            $img= 0;
            
            //$imgdisp =  ceil($imgcnt / 5);
            
            echo "<table align=\"center\" style=\"border:0px solid black\">\n ";                           
            
            while ($row=mysqli_fetch_array($result))
                           
            {
                                 
                  echo"<tr align=\"center\">";
                                    
                  for($i=0; $i<=4;$i++)
                  
                  {
                  
                  echo"<td  style=\"border:1px solid #000; width:25px; padding 4px; text-align:center; align:center;\"> ";
                  
                  echo "<img src= ".$row['imgpath']." width=\"130\"  height=\"130\">&nbsp;&nbsp; ";
                  
                  //echo "".$row['fname']." ";
                  
                  //echo "<br>";
               
                  //echo "<a href=\" ".$row['fname']." \">".$row['fname']." </a> ";
               
                  echo" </td>\n";
                   
                  }
                  
                  echo "</tr> \n ";
                                           
             }   
            
                  echo "</table>";   
         }
         
   ?>

I have 17 images(paths) stored in the table and everything is working fine. I want 5 images on 1 row and so i should get 4 rows
But if i run above code then i get 17 rows. For loop takes 1 image and prints it 5 times in 1 row due to that i get 17 rows. I think i made some mistake in for loop. Can any one rectify that mistake, i will be thankful.

But if i modify my code like below
Code:

$sql = "select * from upload ";
               
                $result = mysqli_query($link,$sql) or die ("Could not access DB: " . mysqli_error());
            
            $imgcnt = mysqli_num_rows($result);
            
            $img= 0;
            
            //$imgdisp =  ceil($imgcnt / 5);
            
            //echo "<table align=\"center\" style=\"border:0px solid black\">\n ";                           
            
            while ($row=mysqli_fetch_array($result))
                           
            {
                                 
                  echo"<tr align=\"center\">";
                                    
            //      for($i=0; $i<=4;$i++)
                  
            //      {
                  
                  echo"<td  style=\"border:1px solid #000; width:25px; padding 4px; text-align:center; align:center;\"> ";
                  
                  echo "<img src= ".$row['imgpath']." width=\"130\"  height=\"130\">&nbsp;&nbsp; ";
                  
                  //echo "".$row['fname']." ";
                  
                  //echo "<br>";
               
                  //echo "<a href=\" ".$row['fname']." \">".$row['fname']." </a> ";
               
                  echo" </td>\n";
                   
                  }
                  
                  echo "</tr> \n ";
                                           
             //}   
            
            //      echo "</table>";   
         }
         
   ?>


If i made changes in the code like above means if i comment "table" tab and for loop then all images are shown in proper way with 4 rows but they dnt come with table borders, or with box to the images , its very surprising that even i made comment to <table> tab the images are shown properly in tabular form.
If i kept above code like that and just uncomment the <table> tag then i get all images in 1 single column. I am very frustrated with this i dnt know wats the problem , i feel i have made some mistakes in "for" loop can any rectify my mistake
Thanks in advance for this urgent help
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 6255
Location: Germany, Next to Hamburg

PostPosted: Mon 29 Jun '09 10:13    Post subject: Reply with quote

I guess you are searching for modulo Wink

This is how it should work (not tested)

Code:

<?php
$sql = "select * from upload ";

$result = mysqli_query($link, $sql) or die ("Could not access DB: " . mysqli_error());


echo '<table align="center" style="border:0px solid black">'."\n ";

$image_count = 0;
while ($row = mysqli_fetch_array($result)) {
   if( (int) $image_count%4==0){
      echo '<tr align="center\>';
   }
   echo '<td  style="border:1px solid #000; width:25px; padding 4px; text-align:center; align:center;">';
   echo '<img src=" ' . $row['imgpath'] . '" width="130"  height="130">&nbsp;&nbsp; ';
   // echo "".$row['fname']." ";
   // echo "<br>";
   // echo "<a href=\" ".$row['fname']." \">".$row['fname']." </a> ";
   echo ' </td>'."\n";
   if( (int) $image_count%4==0){
      echo '</tr> '."\n";
   }
   ++$image_count;
}

echo '</table>';


?>

Back to top
peacemaker



Joined: 23 May 2008
Posts: 80

PostPosted: Mon 29 Jun '09 12:26    Post subject: Problem for loop Reply with quote

Hi James, thanks for such instant reply as usual. I think i m no good in php, if i cant do such simple looping hahhaa,
thanks for reply i will test the code and let you know wats the solution and i wil try to post it if its all done in the forum
thanks a lot
Back to top
peacemaker



Joined: 23 May 2008
Posts: 80

PostPosted: Tue 30 Jun '09 8:07    Post subject: Reply with quote

hi james thanks for the code and it worked for me absolutely fine with some small modifications in the If statements , it worked great for me ,i have just 17 images so its working gr8 for me i wil upload more and then i wil try that code, then i am going to do the pagination then i wil post whole code all in one . At the moment iam giving the corrected code.
Thanks again for the instant reply


James Blond wrote:
I guess you are searching for modulo Wink

This is how it should work (not tested)

Code:

<?php
$sql = "select * from upload ";

$result = mysqli_query($link, $sql) or die ("Could not access DB: " . mysqli_error());


echo '<table align="center" style="border:0px solid black">'."\n ";

[b]$image_count = 0;[/b]
while ($row = mysqli_fetch_array($result)) {
   if( (int) $image_count[b]%5<0[/b]){
      echo '<tr align="center\>';
   }
   echo '<td  style="border:1px solid #000; width:25px; padding 4px; text-align:center; align:center;">';
   echo '<img src=" ' . $row['imgpath'] . '" width="130"  height="130">&nbsp;&nbsp; ';
   // echo "".$row['fname']." ";
   // echo "<br>";
   // echo "<a href=\" ".$row['fname']." \">".$row['fname']." </a> ";
   echo ' </td>'."\n";
   if( (int) $image_count[b]%5[/b]==0){
      echo '</tr> '."\n";
   }
   [b]$image_count++;[/b]
}

echo '</table>';


?>




The code u gave has few chances which i did. I made from %4 to %5 because i need 5 images in one row ,$image_count=1; and i have no idea how it work for doing '<' sign insted of '==' in first if statement and '==' sign for 2nd if statement , but it worked thanks for this i was searching for this since long time.

Following is corrected code

Code:

<?php
$sql = "select * from upload ";

$result = mysqli_query($link, $sql) or die ("Could not access DB: " . mysqli_error());


echo '<table align="center" style="border:0px solid black">'."\n ";

$image_count = 0;
while ($row = mysqli_fetch_array($result)) {
   if( (int) $image_count%4==0){
      echo '<tr align="center\>';
   }
   echo '<td  style="border:1px solid #000; width:25px; padding 4px; text-align:center; align:center;">';
   echo '<img src=" ' . $row['imgpath'] . '" width="130"  height="130">&nbsp;&nbsp; ';
   // echo "".$row['fname']." ";
   // echo "<br>";
   // echo "<a href=\" ".$row['fname']." \">".$row['fname']." </a> ";
   echo ' </td>'."\n";
   if( (int) $image_count%4==0){
      echo '</tr> '."\n";
   }
   $image_count++;
}

echo '</table>';


?>

Back to top


Post new topic   Reply to topic    Apache Forum Index -> Coding & Scripting Corner
Page 1 of 1