Connecting Tech Pros Worldwide Forums | Help | Site Map

Using while loop within another to output multiple strings in a tr

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 5,134
#1: Oct 20 '07
What i want to do:

Get urls from the database and echo them out into a multiple columned table i.e. 4 pictures per row (recently uploaded table)

MY problem is:

I have, in my MySQL database, the urls to the pictures that have been uploaded. I'm not really sure how i would get the urls to go into multiple rows... i can do it as a single row by writing this

Expand|Select|Wrap|Line Numbers
  1. $db = "mahcuz";
  2. $ser = "localhost";
  3. $pass = "";
  4. $usr = "root";
  5. mysql_connect($ser, $usr, $pass) or die ('Error connecting to mysql');
  6. echo "Connected<br />";
  7. mysql_select_db($db) or die('error connection');
  8. echo "db connected<br /><br />";
  9. $query = "SELECT * FROM `ma_pics` WHERE `private` = 'no' ORDER BY `Util_Uploaded` DESC LIMIT 10";
  10. $res = mysql_query($query);
  11. echo "<table>";
  12. while($row = mysql_fetch_array($res)){
  13. echo "<tr>";
  14. echo "<td>".$row['url']."</td>";
  15. echo "</tr>";
  16. }
  17. echo "</table>";
  18.  
But of course, like i said, tha will only print 1 url per row, where as, i want 4!

Any ideas?

I know i could put another while loop within the first one but guidance is needed.

Thanks :)

Newbie
 
Join Date: Oct 2007
Posts: 1
#2: Oct 20 '07

re: Using while loop within another to output multiple strings in a tr


Hey markus,
u hv a partner here with the same problem. i'v geen tryin to figure out a way to do that for weeks now. when i get something i'll tell u. if u do before me then holla.. Happy coding!
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 5,134
#3: Oct 20 '07

re: Using while loop within another to output multiple strings in a tr


This is incredibly annoying ¬_¬ i hate that i can't find a solution... and i bet that solution is really easy too!

:(
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#4: Oct 20 '07

re: Using while loop within another to output multiple strings in a tr


Heya, Markus.

So, we have:
  • Every URL gets enclosed within '<td>' tags.
  • The first URL in each row also has a '<tr>' in front of it.
  • The last URL in each row also has a '</tr>' after it.

If we were to assign each position a unique number:
Expand|Select|Wrap|Line Numbers
  1. 3 columns:
  2. ----------
  3. 0    1    2
  4. 3    4    5
  5. 6    7    8
  6. 9    x    x
  7.  
  8. 5 columns:
  9. ----------
  10. 0    1    2    3    4
  11. 5    6    7    8    9
  12.  
and so on. Notice that the first column in each row is divisible by the number of columns ( x % n === 0 ), and the last column in each row is one minus that ( x % n === n - 1 ).

Putting this into code, we have:

Expand|Select|Wrap|Line Numbers
  1. echo '
  2. <table>';
  3.  
  4. $_counter = -1;  // We start 'outside' the Matrix.  Unlike Neo.
  5. $_cols = 3; // 3 columns.  Of the Ionic variety.
  6. while( $__row = mysql_fetch_assoc($__res) )
  7. {
  8.     // Advance the counter and determine our 'position';
  9.     $_pos = ( ++$_counter % $_cols );
  10.  
  11.     // Should we output a '<tr>'?
  12.     if( $_pos === 0 )
  13.     {
  14.         echo '
  15.         <tr>';
  16.     }
  17.  
  18.     // Output the URL.
  19.     echo "
  20.             <td>
  21.                 {$__row['url']}
  22.             </td>";
  23.  
  24.     // Should we output a '</tr>'?
  25.     if( $_pos === $_cols - 1 )
  26.     {
  27.         echo '
  28.         </tr>';
  29.     }
  30. }
  31.  
But we're not quite done here because the number of URLs might not be neatly divisible, so we have to then output 'dummy' cells (the 'x's in the example above with 3 columns):

Expand|Select|Wrap|Line Numbers
  1. if( $_counter % $_cols !== $_cols - 1 )
  2. {
  3.     do
  4.     {
  5.         echo '
  6.             <td style="visibility: hidden">&nbsp;</td>';
  7.     }
  8.     while( ++$_counter % $_cols !== $_cols - 1 );
  9.  
  10.     echo '
  11.         </tr>';
  12. }
  13.  
  14. echo '
  15. </table>';
  16.  
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 5,134
#5: Oct 20 '07

re: Using while loop within another to output multiple strings in a tr


That, that is just incredible.

I'm seriously in awe of that..

Thanks alot!
Newbie
 
Join Date: Feb 2008
Posts: 22
#6: Feb 27 '08

re: Using while loop within another to output multiple strings in a tr


Quote:

Originally Posted by markusn00b

That, that is just incredible.

I'm seriously in awe of that..

Thanks alot!


could you post the final rulting code. i really wanna see how this turned out for you as i have the same problem
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 5,134
#7: Feb 27 '08

re: Using while loop within another to output multiple strings in a tr


Quote:

Originally Posted by matthewroth

could you post the final rulting code. i really wanna see how this turned out for you as i have the same problem

You'll have to give me 10mins, as this was used a while ago on an old project..
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#8: Feb 27 '08

re: Using while loop within another to output multiple strings in a tr


Quote:

Originally Posted by matthewroth

could you post the final rulting code. i really wanna see how this turned out for you as i have the same problem

Do you realize that you are replying to a post that was made on October 20 2007? So it might never been read.

ROnald
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 5,134
#9: Feb 27 '08

re: Using while loop within another to output multiple strings in a tr


I used it pretty much exactly as the great pbmods gave it:
[php]
// mysql connection and select database...
$_result = mysql_query("
SELECT
`url_short`
FROM
`mahcuz_pics`";
/*
- url_short is the row in which a relative url is held of the uploaded photo
- mahcuz_pics is the table
*/
echo '<table>'; // start the table.
$_counter = -1; // We start 'outside' the Matrix. Unlike Neo.
$_cols = 3; // 3 columns. Of the Ionic variety.
while($_row = mysql_fetch_array($_result)){ //get array of table.
// Advance the counter and determine our 'position';
$_pos = ( ++$_counter % $_cols );

// Should we output a '<tr>'?
if( $_pos === 0 )
{
echo '<tr>';
}

// Output the URL.
echo "
<td>
<a href=\"../../upload/viewer.php?ID={$row['url_short']}\" target=\"_blank\">
<img src=\"../../upload/imgsize.php?w=170&h=130&img=../uploads/{$row['url_short']}\" />
</a>
</td>"; // echoing out the image

// Should we output a '</tr>'?
if( $_pos === $_cols - 1 )
{
echo '</tr>';
}
}
if( $_counter % $_cols !== $_cols - 1 )
{
do
{
echo '<td style="visibility: hidden">&nbsp;</td>';
}
while( ++$_counter % $_cols !== $_cols - 1 );

echo '</tr>';
}

echo '
</table>';
[/php]
Newbie
 
Join Date: Feb 2008
Posts: 22
#10: Feb 27 '08

re: Using while loop within another to output multiple strings in a tr


Quote:

Originally Posted by markusn00b

I used it pretty much exactly as the great pbmods gave it:
[php]
// mysql connection and select database...
$_result = mysql_query("
SELECT
`url_short`
FROM
`mahcuz_pics`";
/*
- url_short is the row in which a relative url is held of the uploaded photo
- mahcuz_pics is the table
*/
echo '<table>'; // start the table.
$_counter = -1; // We start 'outside' the Matrix. Unlike Neo.
$_cols = 3; // 3 columns. Of the Ionic variety.
while($_row = mysql_fetch_array($_result)){ //get array of table.
// Advance the counter and determine our 'position';
$_pos = ( ++$_counter % $_cols );

// Should we output a '<tr>'?
if( $_pos === 0 )
{
echo '<tr>';
}

// Output the URL.
echo "
<td>
<a href=\"../../upload/viewer.php?ID={$row['url_short']}\" target=\"_blank\">
<img src=\"../../upload/imgsize.php?w=170&h=130&img=../uploads/{$row['url_short']}\" />
</a>
</td>"; // echoing out the image

// Should we output a '</tr>'?
if( $_pos === $_cols - 1 )
{
echo '</tr>';
}
}
if( $_counter % $_cols !== $_cols - 1 )
{
do
{
echo '<td style="visibility: hidden">&nbsp;</td>';
}
while( ++$_counter % $_cols !== $_cols - 1 );

echo '</tr>';
}

echo '
</table>';
[/php]

Ok so now I am stuck with 1 final thing. I cant seem to work the code so that each picture has captions above it. The caption would be a text string that says 'Photo Identification' and then displays their last name. this is what i have
[php]
<?
include_once('../sql_connect.php');
$result = mysql_query("SELECT A.LastName, A.FirstName, A.Server, A.TimeIn, A.TimeOut, B.image FROM nocsis A, image B WHERE A.LastName = B.LastName AND A.TimeOut IS NULL ORDER BY A.TimeIn DESC");

{//begin of loop

//now print the results:

echo '<table>'; // start the table.

$_counter = -1; // We start 'outside' the Matrix. Unlike Neo.
$_cols = 5; // 3 columns. Of the Ionic variety.
while($myrow = mysql_fetch_array($result)){ //get array of table.

// Advance the counter and determine our 'position';

$_pos = ( ++$_counter % $_cols );

// Should we output a '<tr>'?

if( $_pos === 0 )
{
echo '<tr>';
}

echo "
<td>

<img src=\"" . $myrow['image']."\" />
</a>
</td>"; // echoing out the image

// Should we output a '</tr>'?
if( $_pos === $_cols - 1 )
{
echo '</tr>';
}
}
if( $_counter % $_cols !== $_cols - 1 )
{
do
{
echo '<td style="visibility: hidden">&nbsp;</td>';
}
while( ++$_counter % $_cols !== $_cols - 1 );

echo '</tr>';
}

echo '
</table>';
} //end of loop

?>
[/php]

and i want to include something like

[php]
echo "<b><u> Photo Identification</b></u> ";

echo "<br>Last Name:&nbsp; &nbsp;";

echo $myrow['LastName'];

echo "<br></br>";

echo "</i><hr align=left width=160>";

echo $myrow['text1'];

[/php]

any ideas??
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#11: Feb 27 '08

re: Using while loop within another to output multiple strings in a tr


You should put that between the <td> and </td> somewhere before the <img statement.

Ronald
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 5,134
#12: Feb 27 '08

re: Using while loop within another to output multiple strings in a tr


Here's what i came up with, it's quite sloppy - just adding a new table inside the td that displays the image:
[php]
// Output the URL.
echo "
<td>
<table>
<tr><td>
{$row['url_short']}
</td></tr>

<tr><td>
<a href=\"../../upload/viewer.php?ID={$row['url_short']}\" target=\"_blank\">
<img src=\"../../upload/imgsize.php?w=170&h=130&img=../uploads/{$row['url_short']}\" />
</a>
</td></tr>
</table>
</td>";
[/php]
Or just do what ronald said..
Newbie
 
Join Date: Feb 2008
Posts: 22
#13: Feb 27 '08

re: Using while loop within another to output multiple strings in a tr


Quote:

Originally Posted by ronverdonk

You should put that between the <td> and </td> somewhere before the <img statement.

Ronald

I must be a retard, i cant seem to get this to work - whats wrong with the code where i try to display Lastname on line 34-39??

Expand|Select|Wrap|Line Numbers
  1. <?
  2. include_once('../sql_connect.php');
  3.     $result = mysql_query("SELECT A.LastName, A.FirstName, A.Server, A.TimeIn, A.TimeOut, B.image  FROM nocsis A, image B WHERE A.LastName = B.LastName AND A.TimeOut IS NULL ORDER BY A.TimeIn DESC");
  4.  
  5.              {//begin of loop
  6.  
  7.                //now print the results:
  8.  
  9.  
  10.  
  11.            echo '<table>'; // start the table.
  12.  
  13.  
  14.  
  15. $_counter = -1;  // We start 'outside' the Matrix.  Unlike Neo.
  16. $_cols = 4; // 4 columns.  Of the Ionic variety.
  17. while($myrow = mysql_fetch_array($result)){ //get array of table.
  18.  
  19.     // Advance the counter and determine our 'position';
  20.  
  21. $_pos = ( ++$_counter % $_cols );
  22.  
  23.     // Should we output a '<tr>'?
  24.  
  25. if( $_pos === 0 )
  26.     {
  27.         echo '<tr>';
  28.     }
  29.  
  30.  
  31.  
  32. echo "
  33.  
  34. <td>
  35.  
  36.  
  37.                echo "<br>Last Name:&nbsp; &nbsp;";
  38.  
  39.                echo $myrow['LastName'];</td>
  40.  
  41.  
  42.  
  43. <img src=\"" . $myrow['image']."\" /></a>
  44. </td>"; // echoing out the image
  45.  
  46.  
  47.  
  48.  
  49. // Should we output a '</tr>'?
  50.     if( $_pos === $_cols - 1 )
  51.     {
  52.         echo '</tr>';
  53.     }
  54. }
  55. if( $_counter % $_cols !== $_cols - 1 )
  56. {
  57.     do
  58.     {
  59.         echo '<td style="visibility: hidden">&nbsp;</td>';
  60.     }
  61.     while( ++$_counter % $_cols !== $_cols - 1 );
  62.  
  63.     echo '</tr>';
  64. }
  65.  
  66. echo '
  67. </table>';
  68.  
  69.  
  70. } //end of loop
  71.  
  72. ?>
  73.  
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 5,134
#14: Feb 27 '08

re: Using while loop within another to output multiple strings in a tr


What does or doesnt happen?
Newbie
 
Join Date: Feb 2008
Posts: 22
#15: Feb 27 '08

re: Using while loop within another to output multiple strings in a tr


Quote:

Originally Posted by markusn00b

What does or doesnt happen?


Parse error: syntax error, unexpected '>' in D:\Program Files\xampp\htdocs\ACS\new_site\test\image1.php on line 37
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#16: Feb 27 '08

re: Using while loop within another to output multiple strings in a tr


Quote:

Originally Posted by matthewroth

Parse error: syntax error, unexpected '>' in D:\Program Files\xampp\htdocs\ACS\new_site\test\image1.php on line 37

You do echoes within an echo operand. It should be like[php]echo "
<td>


<br>Last Name:&nbsp; &nbsp

{$myrow['LastName']}</td>



<img src=\"" . $myrow['image']."\" /></a>
</td>"; // echoing out the image[/php]
Ronald
Reply