473,511 Members | 15,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Markus
6,050 Recognized Expert Expert
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 :)
Oct 20 '07 #1
15 5958
tochie
1 New Member
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!
Oct 20 '07 #2
Markus
6,050 Recognized Expert Expert
This is incredibly annoying ¬_¬ i hate that i can't find a solution... and i bet that solution is really easy too!

:(
Oct 20 '07 #3
pbmods
5,821 Recognized Expert Expert
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.  
Oct 20 '07 #4
Markus
6,050 Recognized Expert Expert
That, that is just incredible.

I'm seriously in awe of that..

Thanks alot!
Oct 20 '07 #5
matthewroth
22 New Member
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
Feb 27 '08 #6
Markus
6,050 Recognized Expert Expert
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..
Feb 27 '08 #7
ronverdonk
4,258 Recognized Expert Specialist
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
Feb 27 '08 #8
Markus
6,050 Recognized Expert Expert
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]
Feb 27 '08 #9
matthewroth
22 New Member
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??
Feb 27 '08 #10
ronverdonk
4,258 Recognized Expert Specialist
You should put that between the <td> and </td> somewhere before the <img statement.

Ronald
Feb 27 '08 #11
Markus
6,050 Recognized Expert Expert
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..
Feb 27 '08 #12
matthewroth
22 New Member
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.  
Feb 27 '08 #13
Markus
6,050 Recognized Expert Expert
What does or doesnt happen?
Feb 27 '08 #14
matthewroth
22 New Member
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
Feb 27 '08 #15
ronverdonk
4,258 Recognized Expert Specialist
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
Feb 27 '08 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

14
2445
by: Crimsonwingz | last post by:
Need to calculate a sum based on a number of factors over a period of years. I can use formula ^x for some of it, but need totals to carry over in the sum and have only been able to do this thus...
1
1857
by: Tor Inge Rislaa | last post by:
Using PowerPoint within my application I am developing an application that needs some functionality of displaying some text and graphic in full screen modus, as when you run a PowerPoint show....
9
2165
by: Diane | last post by:
Could you please explain me how can I output nested strings? Here is an example: "adsd{rfkm}xcv" The output should start from the inner parentheses, such as: dfF rfkm
1
1899
by: David | last post by:
Hi, I'm having trouble copying table data to new records. I have two tables as follows: *** Specifications (Table) specification_ID (field) LINKED product_ID (field) specification_header...
3
2614
by: nico3334 | last post by:
I'm filling in a Report with SQL data using VB code. I'm using LOOP and MoveNext. Before using MoveNext, I would like to be able to check whether the new data is equal to the previous data that was...
5
3747
by: boss1 | last post by:
hi all, i have a problem with loop in select statement.i m using code : <select name = "s" size = "1" > <option selected>P-Code</option> <option...
1
1579
by: gwigg | last post by:
Hi, I am trying to match multiple strings per line from a file and extract them into an array. Apparently the first match is assigned $1, how do I know what the last match is ie $last?? I would like...
0
1494
by: zizi2 | last post by:
Hi, how do I output multiple excel files from one source using vbscript? Regards, Noluthando
4
3763
by: Polarism | last post by:
Hello, I am very new to perl and I am having trouble figuring out how to replace multiple strings in a single file. The file has something around 750k instances that need to be replaced with 350...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.