Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

display 2 columns in php do, while

Posted on 2008-11-09
12
Medium Priority
?
395 Views
Last Modified: 2012-05-05
i'm trying to do this with a table - specifically 2 columns:

<?php do { ?>
          <div id="atBox"><div id="leftImage"><img src="http://joeyrichards.com/images/chinese_art/<?php echo $row_rsPieces['piece_number']; ?>-sm.jpg"></div><div id="rightTxt"><strong><?php echo $row_rsPieces['piece_number']; ?><br><?php echo $row_rsPieces['piece_title']; ?></strong><br><?php echo $row_rsPieces['piece_describe']; ?></div></div><?php } while ($row_rsPieces = mysql_fetch_assoc($rsPieces)); ?>

so something like this:
<table>
<?php do { ?>
       <tr><td><div id="leftImage"><img src="http://joeyrichards.com/images/chinese_art/<?php echo $row_rsPieces['piece_number']; ?>-sm.jpg"></div><div id="rightTxt"><strong><?php echo $row_rsPieces['piece_number']; ?><br><?php echo $row_rsPieces['piece_title']; ?></strong><br><?php echo $row_rsPieces['piece_describe']; ?></div></td></tr><?php } while ($row_rsPieces = mysql_fetch_assoc($rsPieces)); ?></table>

I was doing this with styles but the images aren't all the same height -this is why i think i need table cells, 2 across repeating down the page -

here's a link to the stylesheet version - you can see the problems i'm running into when an image is exceptionally high:

http://healthpresence.com/jr/ancient/
0
Comment
Question by:phillystyle123
  • 6
  • 6
12 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 22917262
Try this:
<?php
  $count = 1;
  echo '<table>';
  while ($row_rsPieces = mysql_fetch_assoc($rsPieces)) {
    $piece_number = $row_rsPieces['piece_number'];
    if($count%2) echo '<tr>';
    echo '<td><div id="leftImage">'.
         '<img src="http://joeyrichards.com/images/chinese_art/'.
         $piece_number.'-sm.jpg"></div>'.
         '<div id="rightTxt"><strong>'.
         $piece_number.'<br>'.
         $row_rsPieces['piece_title'].'</strong><br>'.
         $row_rsPieces['piece_describe'].'</div></td>';
    if(!($count%2)) echo '</tr>';
    $count += 1;
  }
  if(!($count%2)) echo '<td>&nbsp;</td></tr>';
  echo '</table>';
?>

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22917274
There should be an & nbsp; in line 17: '<td>& nbsp;</td></tr>' without the space between & and nbsp! (EE converts this to a space, so I can't write it!)
0
 

Author Comment

by:phillystyle123
ID: 22917564
thanks so much cxr -worked great - i do have another related ? concerning adding some simple javascript to my php code - basically, i can't seen to output singlequotes in the js in my href.

here's a link to that ? if you you're interested:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23889348.html
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:phillystyle123
ID: 22917587
cxr - i just found a glitch in the code - if i'm only outputting 1 record - the record doesn't show in the code

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22917640
That was odd. It works with one record when I test it. Post your code, and I will take a look at it.
0
 

Author Comment

by:phillystyle123
ID: 22917873
<?php
  $count = 1;
  echo '<table align="center" style="width:740px">';
  while ($row_rsPieces = mysql_fetch_assoc($rsPieces)) {
    $piece_number = $row_rsPieces['piece_number'];
    if($count%2) echo '<tr>';
   echo '<td valign="top" style="padding-bottom:10px;">'.
     '<div id="leftImage">'.
         '<a href="#" onClick="MM_openBrWindow(\'popup.php?id='.
         $row_rsPieces['id']."','".$row_rsPieces['piece_number']."','".
         'width='.$row_rsPieces['width'].
         ',height='.$row_rsPieces['height']."')".
         '"><img border="0" '.
         'src="http://joeyrichards.com/images/chinese_art/'.
         $piece_number.'-sm.jpg"></a></div>'.
         '<div id="rightTxt"><strong>'.
         $piece_number.'<br>'.
         $row_rsPieces['piece_title'].'</strong><br>'.
         $row_rsPieces['piece_describe'].
     '</div></td>';
    if(!($count%2)) echo '</tr>';
    $count += 1;
  }
  if(!($count%2)) echo '<td>&nbsp;</td></tr>';
  echo '</table>';
?>
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22917935
I don't have your database, but faking a record I do get the correct output (a single row):
<?php
  # faking a single database record
  $rsPieces = array(
    array('piece_number'=>1,'id'=>1,'width'=>100, 'height'=>100,
          'piece_title'=>'Title', 'piece_describe'=>'Description')
  );
 
  $count = 1;
  echo '<table align="center" style="width:740px">';
  while ($row_rsPieces = array_shift($rsPieces)) {
    $piece_number = $row_rsPieces['piece_number'];
    if($count%2) echo '<tr>';
   echo '<td valign="top" style="padding-bottom:10px;">'.
     '<div id="leftImage">'.
         '<a href="#" onClick="MM_openBrWindow(\'popup.php?id='.
         $row_rsPieces['id']."','".$row_rsPieces['piece_number']."','".
         'width='.$row_rsPieces['width'].
         ',height='.$row_rsPieces['height']."')".
         '"><img border="0" '.
         'src="http://joeyrichards.com/images/chinese_art/'.
         $piece_number.'-sm.jpg"></a></div>'.
         '<div id="rightTxt"><strong>'.
         $piece_number.'<br>'.
         $row_rsPieces['piece_title'].'</strong><br>'.
         $row_rsPieces['piece_describe'].
     '</div></td>';
    if(!($count%2)) echo '</tr>';
    $count += 1;
  }
  if(!($count%2)) echo '<td>&nbsp;</td></tr>';
  echo '</table>';
?>

Open in new window

0
 

Author Comment

by:phillystyle123
ID: 22917962
here's my db - not sure what to do
$colname_rsPieces = "-1";
if (isset($_GET['piece_cat'])) {
  $colname_rsPieces = (get_magic_quotes_gpc()) ? $_GET['piece_cat'] : addslashes($_GET['piece_cat']);
}
mysql_select_db($database_joey, $joey);
$query_rsPieces = sprintf("SELECT id, piece_number, piece_title, piece_describe, piece_cat, order_no, width, height FROM content WHERE piece_cat = %s ORDER BY order_no ASC", GetSQLValueString($colname_rsPieces, "text"));
$rsPieces = mysql_query($query_rsPieces, $joey) or die(mysql_error());
$row_rsPieces = mysql_fetch_assoc($rsPieces);
$totalRows_rsPieces = mysql_num_rows($rsPieces);

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22918088
That is not your database, it is your php code used to read the database. But I think it explains your problem. In line 8, you fetch the first row into the variable $row_rsPieces. That means when the while loop starts to read from the result set using mysql_fetch_assoc($rsPieces), that first row is allready read, and there are no more rows. Remove row 8, and you should be fine.
0
 

Author Comment

by:phillystyle123
ID: 22918125
thanks cxr! every answer has been so on the mark - i really really appreciate it!!!

sql below - thanks again!
$colname_rsPieces = "-1";
if (isset($_GET['piece_cat'])) {
  $colname_rsPieces = (get_magic_quotes_gpc()) ? $_GET['piece_cat'] : addslashes($_GET['piece_cat']);
}
mysql_select_db($database_joey, $joey);
$query_rsPieces = sprintf("SELECT id, piece_number, piece_title, piece_describe, piece_cat, order_no, width, height FROM content WHERE piece_cat = %s ORDER BY order_no ASC", GetSQLValueString($colname_rsPieces, "text"));
$rsPieces = mysql_query($query_rsPieces, $joey) or die(mysql_error());
$totalRows_rsPieces = mysql_num_rows($rsPieces);

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22918210
I assume it works as expected now.

Happy to help! :)
0
 

Author Comment

by:phillystyle123
ID: 22918217
all working great and i learned a lot in the process - thanks again
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

575 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question