Jump to content

Help with results in multiple columns


lingo5

Recommended Posts

Hi,

I have this query to display results in one column and multiple rows:

$query_asociados_RS = "SELECT * FROM t_empresas ORDER BY E_nombre ASC";
$asociados_RS = mysql_query($query_asociados_RS, $amat_connect) or die(mysql_error());
$row_asociados_RS = mysql_fetch_assoc($asociados_RS);
$totalRows_asociados_RS = mysql_num_rows($asociados_RS);

$queryString_asociados_RS = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_asociados_RS") == false && 
        stristr($param, "totalRows_asociados_RS") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_asociados_RS = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_asociados_RS = sprintf("&totalRows_asociados_RS=%d%s", $totalRows_asociados_RS, $queryString_asociados_RS);
?>

 

It works fine, but I need to display the results in 6 columns and multiple rows. I don't know how to do this, so any help would be very much appreciated.

Thanks all.

Link to comment
https://forums.phpfreaks.com/topic/198289-help-with-results-in-multiple-columns/
Share on other sites

I have no idea what you're talking about. You need to be more specific. The query only returns however many columns you have in the table. If you want to return 6 columns, what are the columns for? Your post is just not specific enough and it doesn't give us any information regarding what your issue.

 

I can be a wise guy and just write a query that returns 6 columns with result, but I doubt they are the result you're looking for. So please elaborate.

Hi Ken, sorry about that.

What that query returns is 1 row for each registry on the DB like this:

 

associate 1

associate 2

associate 3

 

etc....

 

I would like the rsults to show like this:

 

associate 1  | associate 2  | associate 3

 

associate 4  |  associate 5  | associate 6

 

etc...

 

Is this any clearer now?. Thanks

Well, that's a display problem, not a query problem.

 

Let's say the results of the query is stored in the variable $results. Then you can loop through it as such:

 

<table><tr>
<?php
$cols_per_row = 3;
$count = 1;
while ($result = mysql_fetch_assoc($results)) {
     if ($count == $cols_per_row) {
          $count = 1;
          echo "</tr><tr>";
     }
     $value = $results['col_name'];
     if (empty(trim($value))) $value = " ";
     echo sprintf("<td>%s</td>", $value);
     $count++;
}

while ($count < $cols_per_row) echo "<td> </td>";
?>
</tr></table>

 

Does that make some sense?

at the moment this is how I display my results:

                                <?php do { ?>
                                  <table width="100%" border="0" cellpadding="11" cellspacing="0" class="asociadoBottomBorder">
                                    <tr<?php >
                                      <td width="21%" align="left" valign="middle"   class="BodyText" ><div align="left"><a href="detalle_asociados.php?id_E=<?php echo $row_asociados_RS['id_E']; ?>"><img src="uploads/<?php echo $row_asociados_RS['E_logo']; ?>" alt="" width="150" border="1" /></a></div></td>
                                      <td width="79%" height="60" align="left" valign="middle"   class="BodyText"><a href="detalle_asociados.php?id_E=<?php echo $row_asociados_RS['id_E']; ?>" class="nombreFichaAsociado"><?php echo $row_asociados_RS['E_nombre']; ?></a></td>
                                    </tr>

                                  </table>
                                  <?php } while ($row_asociados_RS = mysql_fetch_assoc($asociados_RS)); ?></td>
                              </tr>
  
?>
       

Thanks Ken2k7, but I don't quite understand how to use that code. Could you please have a look at the codeI use at the moment to display the results and help me implement the one you suggested?

am really stuck with this. Thanks again.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.