PDA

View Full Version : do while but 3 items per row


gavimobile
06-02-2008, 10:41 AM
hey i have an easy one for you guys. right now i have a list pulling from the database. I designed it with 3 items per row, the only problem is each row display one item, the next row displays the next item. eg.

id#1 id#1 id#1

id#2 id#2 id#2

id#3 id#3 id#3

i want

id#1 id#2 id#3

id#4 id#5 id#6

id#7 id#8 id#9

this is the short version of what i got



<?
require_once('includes/config.php');

/*item database query*/
$query = sprintf("SELECT * FROM item");
$results = @mysql_query($query);
$row_item = @mysql_fetch_array($results);

do {

?>
<div id="box-content">

<div id="left-col">
<b class="b1"></b><b class="b2"></b><b class="b3"></b><b class="b4"></b>
<div class="contentb">
<div><? echo "<img src='images/item/" . $row_item['item_pic'] . "' />"; ?></div>
<div id="title"><? echo $row_item['item_name']; ?></div>
<div id="price"><? echo "$" . $row_item['item_price']; ?></div>
<input class="Details-btn" type="submit" value="Details" />
<input class="AddToCart-btn" type="submit" value="Add To Cart" />
</div>
<b class="b4"></b><b class="b3"></b><b class="b2"></b><b class="b1"></b>
</div>

<div id="mid-col">
<b class="b1"></b><b class="b2"></b><b class="b3"></b><b class="b4"></b>
<div class="contentb">
<div><? echo "<img src='images/item/" . $row_item['item_pic'] . "' />"; ?></div>
<div id="title"><? echo $row_item['item_name']; ?></div>
<div id="price"><? echo "$" . $row_item['item_price']; ?></div>
<input class="Details-btn" type="submit" value="Details" />
<input class="AddToCart-btn" type="submit" value="Add To Cart" />
</div>
<b class="b4"></b><b class="b3"></b><b class="b2"></b><b class="b1"></b>
</div>

<div id="right-col">
<b class="b1"></b><b class="b2"></b><b class="b3"></b><b class="b4"></b>
<div class="contentb">
<div><? echo "<img src='images/item/" . $row_item['item_pic'] . "' />"; ?></div>
<div id="title"><? echo $row_item['item_name']; ?></div>
<div id="price"><? echo "$" . $row_item['item_price']; ?></div>
<input class="Details-btn" type="submit" value="Details" />
<input class="AddToCart-btn" type="submit" value="Add To Cart" />
</div>
<b class="b4"></b><b class="b3"></b><b class="b2"></b><b class="b1"></b>
</div>


<?
}
while ($row_item = @mysql_fetch_array($results));
?>


i hope this isnt to messy, if you wish for me to clean it up more let me know ;-)

tia

lux
06-02-2008, 03:01 PM
<?
require_once('includes/config.php');

/*item database query*/
$query = sprintf("SELECT * FROM item");
$results = @mysql_query($query);


while($row_item = @mysql_fetch_array($results)){
echo "<p>";
echo $row_item['item_pic']."<br />";
echo $row_item['item_name']."<br />";
echo "$".$row_item['item_price']."<br />";
echo "</p>";
}

?>


add your html where required.

also look up the difference between "do while loops" and "while loops".

post again if you have any more problems

gavimobile
06-02-2008, 08:28 PM
<?
require_once('includes/config.php');

/*item database query*/
$query = sprintf("SELECT * FROM item");
$results = @mysql_query($query);


while($row_item = @mysql_fetch_array($results)){
echo "<p>";
echo $row_item['item_pic']."<br />";
echo $row_item['item_name']."<br />";
echo "$".$row_item['item_price']."<br />";
echo "</p>";
}

?>
add your html where required.

also look up the difference between "do while loops" and "while loops".

post again if you have any more problems

forgive me lux for not following..

i looked up do while and while loops and it seems it controls the variable from when untill where i want, but i still dont see how to make this work horizontally.
give me another hint!!!

i appreciate your time

davidj
06-02-2008, 09:04 PM
so are you wanting the loop to draw 3 cells (td's) then automatically start the next row

gavimobile
06-02-2008, 11:01 PM
so are you wanting the loop to draw 3 cells (td's) then automatically start the next row

was that a question? or was that my hint?

davidj
06-03-2008, 06:04 AM
im trying to understand your requirements

gavimobile
06-03-2008, 06:21 AM
im trying to understand your requirements
good morning and excuese me for not explaining myself correctly.
in your dwc tutorial when i pull a list from a database it gets sorted out like this.

1<br />
2<br />
3<br />
4 <br />

but what i want is

1 2 3 <br />
4 5 6 <br />
7 8 9 <br />

the problem is it is comming out like this

1 1 1
2 2 2
3 3 3

this happened because my entire row is wraped in a loop and i am not sure how one would go about saying i want 3 items in a row and than tell the php to loop that.

was this clear?

thanks dj

lux
06-03-2008, 08:41 AM
just to make sure i understand you want to output:

item_pic item_name item_price
item_pic item_name item_price
item_pic item_name item_price
item_pic item_name item_price

if so take my example and add the <tr></tr> and <td></td> tags where required
or do you want to see item_pic, item_name, item_price all in one <td></td> ?? and then display 3 table cells per row?

davidj
06-03-2008, 10:15 AM
Lux

he wants it to loop and create a matrix grid with each cell containing data

the loop will know when to create a new row after its completed 3 td's

lux
06-03-2008, 10:25 AM
cool

heres an example to work through:

http://www.wellho.net/resources/ex.php4?item=h106/5pl.php

gavimobile
06-03-2008, 04:16 PM
cool

heres an example to work through:

http://www.wellho.net/resources/ex.php4?item=h106/5pl.php

yyes!! this is exactly what im looking for!

now i just need to find out how to modify this to work with mysql. ive never worked with arrays yet. Im not sure what i will need to read up on to turn this code to fit my needs. where and what am i looking for on good ole google? do you have any links you think might help me?

thanks folks

btw im expirencing slowness with dreamweaverclub.com other websites are going fine but here its innermittant normal and slow, any ideas?

davidj
06-03-2008, 05:04 PM
you dont need it

set a count variable which increments through the loop

divide the count var by 3 and check if the result is not a float

if it is a float then use a <td> if its a whole number then add a </tr><tr>

gavimobile
06-11-2008, 06:02 PM
ok this is solved!

i found an excellent website which layed it out for me exactly as i wanted it.

here is the code

<table border=5>
<?
$item_per_row = 3;
$content = '<tr>';
$sql = "SELECT name FROM table";
$rows = mysql_query($sql);
if (mysql_num_rows($rows) == 0) {
$content = '<td>No items</td></tr>';
} else {
$count = 0;
while ($row = mysql_fetch_assoc($rows)) {
$content .= '<td>' . $row['name'] .'</td>';
if (++$count % $item_per_row === 0) {
print $content .= '</tr>';
}
}
if ($count % $item_per_row!== 0) {
$content .= '</td>';
}
}
?>
</table>

and i got it from here http://www.webmasterworld.com/forum88/5254.htm


to thoese who have replied thanks for your help anyways, Ive tried very hard to understand but i was unsucessful. please dont take it as a slap in the face ;-) you guys are great!