Page 1 of 1

[SOLVED] Looking for feedback for improving this code

Posted: Sat Jan 29, 2005 3:10 am
by mhenke
Hi,

This code should produce a table, but it doesn't produce anything:

Code: Select all

<?
$cols = 4; 
$arrayofitems = array('a','b','c','d','e','f','g','h','i'); 
$arrayofitems2 = array('description of a','description of b','description of c','description of d','description of e','description of f','description of g','description of h','description of i'); 
$rows = ceil(sizeof($arrayofitems) / $cols); 
$counter = 0; 

$content = '<table border=1 cellpadding=5>'; 

for($i = 0; $i < $rows; $i++) &#123; 

    $content .= '<tr>'; 
     
    for($j = 0; $j < $cols; $j++) &#123; 
         
        if(!empty($arrayofitems&#1111;$counter])) &#123; 
             
            $content .= '<td align="center"><strong>'.$arrayofitems&#1111;$counter].'</strong></td>'; 
        &#125; 
        else &#123; 
             
            $content .= '<td>&nbsp;</td>'; 
        &#125; 

        $counter++; 
    &#125; 

    $content .= '</tr>'; 

    $counter = $counter - $cols; 

    $content .= '<tr>'; 
     
    for($j = 0; $j < $cols; $j++) &#123; 
         
        if(!empty($arrayofitems&#1111;$counter])) &#123; 
             
            $content .= '<td>Column '.$arrayofitems2&#1111;$counter].'</td>'; 
        &#125; 
        else &#123; 
             
            $content .= '<td>&nbsp;</td>'; 
        &#125; 

        $counter++; 
    &#125; 

    $content .= '</tr>'; 
&#125; 

?>
The table it should produce:

Code: Select all

<table border="1" cellpadding="5">
      <tr>
        <td align="center">
          <strong>a</strong>
        </td>
        <td align="center">
          <strong>b</strong>
        </td>
        <td align="center">
          <strong>c</strong>
        </td>
        <td align="center">
          <strong>d</strong>
        </td>
      </tr>
      <tr>
        <td>
          Column description of a
        </td>
        <td>
          Column description of b
        </td>
        <td>
          Column description of c
        </td>
        <td>
          Column description of d
        </td>
      </tr>
      <tr>
        <td align="center">
          <strong>e</strong>
        </td>
        <td align="center">
          <strong>f</strong>
        </td>
        <td align="center">
          <strong>g</strong>
        </td>
        <td align="center">
          <strong>h</strong>
        </td>
      </tr>
      <tr>
        <td>
          Column description of e
        </td>
        <td>
          Column description of f
        </td>
        <td>
          Column description of g
        </td>
        <td>
          Column description of h
        </td>
      </tr>
      <tr>
        <td align="center">
          <strong>i</strong>
        </td>
        <td>
           
        </td>
        <td>
           
        </td>
        <td>
           
        </td>
      </tr>
      <tr>
        <td>
          Column description of i
        </td>
        <td>
           
        </td>
        <td>
           
        </td>
        <td>
           
        </td>
      </tr>
    </table>

Posted: Sat Jan 29, 2005 4:20 am
by hunterhp
Nothing is wrong, you just haven't echoed $content

echo "$content";

Posted: Sat Jan 29, 2005 4:57 am
by mhenke
Ooops... guess I didn't have my lenses in ; )

THX!

Posted: Sat Jan 29, 2005 5:09 am
by hunterhp
I didn't see it either, I was testing the code on my server for a while, changing different little things. Then I just noticed it. Pretty cool code though, I like simple and effective codes like that.

Posted: Mon Jan 31, 2005 3:45 am
by mhenke
It is very cool, managed to do lots of fun things with it. Used it with file searches (files in dir using glob), but now I'm trying to display results from db, and for some reason can't get it to work : (

It only returns 1 username in the first column, instead of 4 colums filled with 4 different usernames... Whatever I try, the remaining columns are all filled with '&nbsp;' so it cannot find more results. What am I doing wrong?

Code: Select all

<?
dbuser();
$query = "SELECT username FROM users";
$result = mysql_query($query);
$prof = mysql_num_rows($result);

$cols = 4; 
$rows = ceil($prof / $cols);
$counter = 0; 
$content = '<table border=1 cellpadding=5>'; 
for($i = 0; $i < $rows; $i++) &#123; 

$row = mysql_fetch_row($result);

    $content .= '<tr>'; 
     
    for($j = 0; $j < $cols; $j++) &#123; 
	
	        if(!empty($row&#1111;$counter])) &#123; 
		        
            $content .= '<td align="center"><strong>'.$row&#1111;$counter].'</strong></td>'; 
       &#125; 
       else &#123; 
             
           $content .= '<td>&nbsp;</td>'; 
       &#125; 

        $counter++; 
    &#125; 

    $content .= '</tr>'; 

    $counter = $counter - $cols; 

    $content .= '<tr>'; 
     
    for($j = 0; $j < $cols; $j++) &#123; 
	        
        if(!empty($row&#1111;$counter])) &#123; 
		
            $content .= '<td>'.$row&#1111;$counter].'</td>'; 
        &#125; 
       else &#123; 
             
            $content .= '<td>&nbsp;</td>'; 
        &#125; 

      $counter++; 
 &#125; 

  $content .= '</tr>'; 
&#125;
echo "$content";
?>

Posted: Mon Jan 31, 2005 5:01 am
by mhenke
SOLVED! Searched php.net again and found that I had to use mysql_result:

Code: Select all

<?
dbuser();
$query = "SELECT username FROM users ";
$result = mysql_query($query);
$prof = mysql_num_rows($result);

$cols = 4; 
$rows = ceil($prof / $cols);
$counter = 0; 
$content = '<table border=1 cellpadding=5>'; 
for($i = 0; $i < $rows; $i++) &#123;

    $content .= '<tr>'; 
     
    for($j = 0; $j < $cols; $j++) &#123;
	
	$row = mysql_result($result,$counter);
	
	        if(!empty($row&#1111;$counter])) &#123; 
		        
            $content .= '<td align="center"><strong>'.$row.'</strong></td>'; 
       &#125; 
       else &#123; 
             
           $content .= '<td>&nbsp;</td>'; 
       &#125; 

        $counter++; 
    &#125; 

    $content .= '</tr>'; 

    $counter = $counter - $cols; 

    $content .= '<tr>'; 
     
    for($j = 0; $j < $cols; $j++) &#123; 
	
	$link = mysql_result($result,$counter);
        
        if(!empty($link&#1111;$counter])) &#123; 
		
            $content .= '<td>'.$link.'</td>'; 
        &#125; 
       else &#123; 
             
            $content .= '<td>&nbsp;</td>'; 
        &#125; 

      $counter++; 
 &#125; 

  $content .= '</tr>'; 
&#125;
echo "$content";
?>
I would like to know if this code could be improved, as it says that using mysql_result is very slow. Eventually this code should work its way through a huge db...

Posted: Mon Jan 31, 2005 7:10 am
by timvw
you probably want to make a table a function.

this way, you only have to pass it an array with rows.. (doesn't matter if those come from glob or a mysql query... it just needs rows :p)

Code: Select all

$query = ".....";
$result = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR);
$rows = array();
while ($row = mysql_fetch_row($result))
&#123;
     $rows&#1111;] = $row;
&#125;

echo buildTable($rows);

function buildTable($datarows, $cols = 4)
&#123;

// build your table here
return $table;

&#125;

Posted: Mon Jan 31, 2005 7:44 am
by mhenke
THX! Gonna make it work now : )

Posted: Mon Jan 31, 2005 9:30 am
by mhenke
Here's the full code, works like a charm! THX timvw!!!!! : )

Code: Select all

<?
dbuser();
$query = "SELECT username, country, city_1 FROM users "; 
$result = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR); 
$rows = array(); 
while ($row = mysql_fetch_row($result)) 
&#123; 
     $rows&#1111;] = $row; 
&#125; 

echo buildTable($rows); 

function buildTable($rows) 
&#123; 

$cols = 3; 
$numrows = ceil(count($rows) / $cols);

$counter = 0; 

$table = '<table border=0 cellspacing=5>'; 
for($i = 0; $i < $numrows; $i++) &#123;

    $table .= '<tr>'; 
     
    for($j = 0; $j < $cols; $j++) &#123;
	
				     if(!empty($rows&#1111;$counter])) &#123; 
		        
            $table .= '<td align="center" class="td2"><img src="../emb/b/'.$rows&#1111;$counter]&#1111;0].'_tn.jpg" border="0"></td>'; 
       &#125; 
       else &#123; 
             
           $table .= '<td>&nbsp;</td>'; 
       &#125; 

        $counter++; 
    &#125; 

    $table .= '</tr>'; 

    $counter = $counter - $cols; 

    $table .= '<tr>'; 
     
    for($j = 0; $j < $cols; $j++) &#123; 
	
	        
        if(!empty($rows&#1111;$counter])) &#123; 
		
            $table .= '<td align="center" class="td2">'.$rows&#1111;$counter]&#1111;1].'<br>'.$rows&#1111;$counter]&#1111;2].'</td>'; 
        &#125; 
       else &#123; 
             
            $table .= '<td>&nbsp;</td>'; 
        &#125; 

      $counter++; 
 &#125; 

  $table .= '</tr><tr><td><img src="../gfx/spacer.gif" height="5"></td></tr>'; 
&#125;

$table .= '</table>'; 

return $table; 

&#125; 
?>