output mysql into divs not a table

Soldato
Joined
7 Jan 2007
Posts
10,607
Location
Sussex, UK
How do I output the results into 3 container divs? with each of the divs assigned their own div so I can manipulate company, product and price later with css?

PHP:
        $query =    ("SELECT * 
                        FROM company
                        JOIN products
                        ON company.idcompany = products.idcompany JOIN price
                        ON products.idProducts = price.idproduct
                        ORDER BY price DESC
                        LIMIT 3");
                    
                    
$result = mysql_query($query) or die(mysql_error());

// Print out the contents of each row into divs
while($row = mysql_fetch_array($result)){
    echo $row['company]. " ". $row['product']. " ". $row['price'];


}
 
Yeh sure,

http://overclockers.co.uk/

On the OCUK home page you have the container div underneath the javascript slider with 3 seperate ones for bundles, systems and portables. I know ocuk have put there data into a table but I would like something similar for my site but in divs not a table.

essentially I want to create this

<div class="top3">

<div class="company">company 1 output</div>

<div class="product">product 1 output</div>

<div class="price">price 1 output</div>

</div>

<div class="top3">


<div class="company">company 2 output</div>

<div class="product">product 2 output</div>

<div class="price">price 2 output</div>
</div>

<div class="top3">

<div class="company">company 3 output</div>

<div class="product">product 3 output</div>

<div class="price">price 3 output</div>

</div>
 
I thought you wanted something more complicated :)

Code:
	while($row = mysql_fetch_array($result)) {

		$html = <<<EOF
<div class="top3">

<div class="company">{$row['company']}</div>

<div class="product">{$row['product']}</div>

<div class="price">{$row['price']}</div>

</div>
EOF;
		echo $html;
	}
 
For future reference, how would you output the query results into 2 divs side by side, so the output is alternating between the two?

Example:

Code:
Div1:               Div2:

Company 1         Company 2
Product 1         Product 2
Price 1             Price 2

Company 3         Company 4
Product 3         Product 4
Price 3             Price 4

Thanks.
 
Steve09 - you simply need to a toggle function to alternate between various values.

Code:
	// Toggles between all values in $vals
	function toggle(&$var, $vals = array()) {
		if (!is_array($vals) || empty($vals))
			return $var;
		$cnt = count($vals);
		for ($i=0;$i<$cnt;$i++)
			if ($vals[$i]==$var) {
				break;	
		}
		$i++;
		if ($i>=$cnt)
			$i = 0;
		$var = $vals[$i];
		return $var;
	}

And how it would be used:
Code:
	$div = '';
	$div_list = array('div_left_float','div_right_float');
	
	while($row = mysql_fetch_array($result)) {
		toggle($div,$div_list);
		$html = <<<EOF
<div class="{$div}">

<div class="company">{$row['company']}</div>

<div class="product">{$row['product']}</div>

<div class="price">{$row['price']}</div>

</div>
EOF;
		echo $html;
	}
 
Although on further reading it does appear you want to use just 2 divs side by side containing the entire column. For this you'd need to buffer the data for latter output. Something like:

Code:
	$divs = array();
	$cur_div = '';
	$div_list = array('left','right');
	foreach ($div_list as $div)
		$divs[$div] = array();
	
	while($row = mysql_fetch_array($result)) {
		toggle($cur_div,$div_list);
		$div[$cur_div] .= <<<EOF
<div class="company">{$row['company']}</div>

<div class="product">{$row['product']}</div>

<div class="price">{$row['price']}</div>

EOF;
	}
	
	foreach ($divs as $class => $data) {
		echo '<div class=\"'.$class.'\">';
		echo $data;
		echo '</div>';
	}
 
Back
Top Bottom