473,421 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

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

Markus
6,050 Expert 4TB
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 5945
tochie
1
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 Expert 4TB
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 Expert 4TB
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 Expert 4TB
That, that is just incredible.

I'm seriously in awe of that..

Thanks alot!
Oct 20 '07 #5
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 Expert 4TB
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 Expert 4TB
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 Expert 4TB
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
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 Expert 4TB
You should put that between the <td> and </td> somewhere before the <img statement.

Ronald
Feb 27 '08 #11
Markus
6,050 Expert 4TB
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
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 Expert 4TB
What does or doesnt happen?
Feb 27 '08 #14
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 Expert 4TB
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
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
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
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
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
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
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
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
by: zizi2 | last post by:
Hi, how do I output multiple excel files from one source using vbscript? Regards, Noluthando
4
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.