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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #1

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

    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

    [code=php]
    $db = "mahcuz";
    $ser = "localhost" ;
    $pass = "";
    $usr = "root";
    mysql_connect($ ser, $usr, $pass) or die ('Error connecting to mysql');
    echo "Connected< br />";
    mysql_select_db ($db) or die('error connection');
    echo "db connected<br /><br />";
    $query = "SELECT * FROM `ma_pics` WHERE `private` = 'no' ORDER BY `Util_Uploaded` DESC LIMIT 10";
    $res = mysql_query($qu ery);
    echo "<table>";
    while($row = mysql_fetch_arr ay($res)){
    echo "<tr>";
    echo "<td>".$row['url']."</td>";
    echo "</tr>";
    }
    echo "</table>";
    [/code]
    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 :)
    Last edited by Markus; Oct 20 '07, 02:38 PM. Reason: Title
  • tochie
    New Member
    • Oct 2007
    • 1

    #2
    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!
    Last edited by pbmods; Oct 20 '07, 06:44 PM. Reason: Removed email.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

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

      :(

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        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:
        Code:
        3 columns:
        ----------
        0    1    2
        3    4    5
        6    7    8
        9    x    x
        
        5 columns:
        ----------
        0    1    2    3    4
        5    6    7    8    9
        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:

        [code=php]
        echo '
        <table>';

        $_counter = -1; // We start 'outside' the Matrix. Unlike Neo.
        $_cols = 3; // 3 columns. Of the Ionic variety.
        while( $__row = mysql_fetch_ass oc($__res) )
        {
        // 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>
        {$__row['url']}
        </td>";

        // Should we output a '</tr>'?
        if( $_pos === $_cols - 1 )
        {
        echo '
        </tr>';
        }
        }
        [/code]

        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):

        [code=php]
        if( $_counter % $_cols !== $_cols - 1 )
        {
        do
        {
        echo '
        <td style="visibili ty: hidden">&nbsp;</td>';
        }
        while( ++$_counter % $_cols !== $_cols - 1 );

        echo '
        </tr>';
        }

        echo '
        </table>';
        [/code]
        Last edited by pbmods; Oct 31 '07, 04:01 PM. Reason: Added missing semicolon.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          That, that is just incredible.

          I'm seriously in awe of that..

          Thanks alot!

          Comment

          • matthewroth
            New Member
            • Feb 2008
            • 22

            #6
            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

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              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..

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                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

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #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_arr ay($_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=1 70&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="visibili ty: hidden">&nbsp;</td>';
                  }
                  while( ++$_counter % $_cols !== $_cols - 1 );

                  echo '</tr>';
                  }

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

                  Comment

                  • matthewroth
                    New Member
                    • Feb 2008
                    • 22

                    #10
                    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_arr ay($_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=1 70&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="visibili ty: 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("SE LECT 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_arr ay($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="visibili ty: 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??
                    Last edited by ronverdonk; Feb 27 '08, 05:02 PM. Reason: use PHP code tags

                    Comment

                    • ronverdonk
                      Recognized Expert Specialist
                      • Jul 2006
                      • 4259

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

                      Ronald

                      Comment

                      • Markus
                        Recognized Expert Expert
                        • Jun 2007
                        • 6092

                        #12
                        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=1 70&h=130&img=. ./uploads/{$row['url_short']}\" />
                        </a>
                        </td></tr>
                        </table>
                        </td>";
                        [/php]
                        Or just do what ronald said..

                        Comment

                        • matthewroth
                          New Member
                          • Feb 2008
                          • 22

                          #13
                          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??

                          Code:
                          <?
                          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 = 4; // 4 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>
                          
                          
                                         echo "<br>Last Name:&nbsp; &nbsp;";
                          
                                         echo $myrow['LastName'];</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
                           
                          ?>

                          Comment

                          • Markus
                            Recognized Expert Expert
                            • Jun 2007
                            • 6092

                            #14
                            What does or doesnt happen?

                            Comment

                            • matthewroth
                              New Member
                              • Feb 2008
                              • 22

                              #15
                              Originally posted by markusn00b
                              What does or doesnt happen?

                              Parse error: syntax error, unexpected '>' in D:\Program Files\xampp\htd ocs\ACS\new_sit e\test\image1.p hp on line 37

                              Comment

                              Working...