/    Sign up×
Community /Pin to ProfileBookmark

For within While loop

I have a while loop which calls data from a db and displays it within a table.

However, the table is stretching too far across my page.

So I did a test with a second while loop so that if $i < 6 then a new row is added.

However when I nested one loop inside the other my WAMP, unsuprisingly, hated this and never loaded the page.

So I replaced the nested while loop with for loop instead.

This does then change the table however, it also repeats everything in the table 7 times instead of just the once.

[code=php]echo ‘<table border=”1″>
<tr>’;
$query = “select image from wp_wpsc_variation_values where variation_id = ‘1’”;
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
for($i = 1; $i < 8; $i++){
if($i == 7) {
echo “<tr>”;
}
echo ‘<td>
<input type=”radio” value=”<?php echo wpsc_the_variation_id(); ?>” class=”wpsc_select_variation” name=”variation[<?php echo wpsc_vargrp_id(); ?>]” id=”<?php echo wpsc_vargrp_form_id(); ?>”></td>’;
echo “<td><img src=’http://www.site.co.uk/wordpress/wp-content/plugins/wp-e-commerce/themes/lsotheme/variationimgs/$row[image]’ /></td>”;
if($i == 7) {
echo “</tr>”;
}
}
}

echo ‘</tr>
</table>’;[/code]

Thats the code I’m using, so each picture is now displaying 7 times where I want it to display once. But each set of 7 pictures are displaying correctly.

[URL=”http://i972.photobucket.com/albums/ae207/Aravona/3.png”]http://i972.photobucket.com/albums/ae207/Aravona/3.png[/URL]

That shows what I mean a bit better.

Any help would be wonderful.

Regards,

Aravona

to post a comment
PHP

24 Comments(s)

Copy linkTweet thisAlerts:
@Peterk92Nov 24.2010 — [code=php]echo '<table border="1">
<tr>';
$query = "select image from wp_wpsc_variation_values where variation_id = '1'";
$result = mysql_query($query);
$i = "0";
while($row = mysql_fetch_array($result)) {
if($i == 7) {
echo "<tr>";
}
echo '<td>
<input type="radio" value="<?php echo wpsc_the_variation_id(); ?>" class="wpsc_select_variation" name="variation[<?php echo wpsc_vargrp_id(); ?>]" id="<?php echo wpsc_vargrp_form_id(); ?>"></td>';
echo "<td><img src='http://www.site.co.uk/wordpress/wp-content/plugins/wp-e-commerce/themes/lsotheme/variationimgs/$row[image]' /></td>";
if($i == 7) {
echo "</tr>";
$i = "0";
}else if($i != "7"){ $i++; }
}

echo '</tr>
</table>';[/code]


Not sure but try that.
Copy linkTweet thisAlerts:
@AravonaauthorNov 24.2010 — That worked, mostly.

It looks a lot better I think I just need to tweak the $i values now.

Alignment is perfect! Thank you ?

Edit:

Actually there is a problem with it but I'm not sure why. I went to cut $i to 5. but now it looks like this:

http://i972.photobucket.com/albums/ae207/Aravona/4.png
Copy linkTweet thisAlerts:
@criterion9Nov 24.2010 — I usually use modulus (%) for stuff like this.

Something like the following (untested):
[code=php]
$nums = range(1,25);
echo "<table><tbody><tr>";
$i = 0;
$cols = 3;
$thisRowCols = 0;
foreach($nums as $num){
$i++;
if($i%$cols == 0){
echo '</tr><tr>';
$thisRowCols = 0;
}
echo "<td>$num</td>";
$thisRowCols++;
}
if($thisRowCols < $cols){
//we need to add empty columns to finish filling the row
while($thisRowCols < $cols){
echo '<td></td>';
$thisRowCols++;
}
echo '</tr>';
}
echo "</tbody></table>";
[/code]
Copy linkTweet thisAlerts:
@AravonaauthorNov 24.2010 — I've never used modulus before.

I dont see where I would put the preset coding from my plugin in here?
Copy linkTweet thisAlerts:
@criterion9Nov 24.2010 — switch thiswhile($row = mysql_fetch_array($result)) {[/quote]

where I have this
foreach($nums as $num){[/quote]


And put your td and contents where i have

echo "<td>$num</td>";
[/quote]
Copy linkTweet thisAlerts:
@AravonaauthorNov 24.2010 — I don't see how I can use $num when I have several aspects being called with many functions?

So do you replace $num with :

[code=php]$nums = range(1,25);
echo "<table><tbody><tr>";
$i = 0;
$cols = 3;
$thisRowCols = 0;
foreach($nums as $num){
$i++;
if($i%$cols == 0){
echo '</tr><tr>';
$thisRowCols = 0;
}
echo '<td>
<input type="radio" value="<?php echo wpsc_the_variation_id(); ?>" class="wpsc_select_variation" name="variation[<?php echo wpsc_vargrp_id(); ?>]" id="<?php echo wpsc_vargrp_form_id(); ?>"></td>';
echo "<td><img src='http://www.samleisure.co.uk/wordpress/wp-content/plugins/wp-e-commerce/themes/lsotheme/variationimgs/$row[image]' /></td>";

$thisRowCols++;
}
if($thisRowCols < $cols){
//we need to add empty columns to finish filling the row
while($thisRowCols < $cols){
echo '<td></td>';
$thisRowCols++;
}
echo '</tr>';
} [/code]


?? Is that what you mean?

I also don't understand why you have: $nums = range(1,25);
Copy linkTweet thisAlerts:
@criterion9Nov 24.2010 — $nums = range(1,25); was to build an array of numbers from 1-25 to illustrate the rest of the code.

[code=php]
<?php
//don't forget to include your query here
echo "<table><tbody><tr>";
$i = 0;
$cols = 3;
$thisRowCols = 0;
foreach($nums as $num){ //forgot to switch this out for your code
$i++;
if($i &#37; $cols == 0){
echo '</tr><tr>';
$thisRowCols = 0;
}
echo '<td>
<input type="radio" value="<?php echo wpsc_the_variation_id(); ?>" class="wpsc_select_variation" name="variation[<?php echo wpsc_vargrp_id(); ?>]" id="<?php echo wpsc_vargrp_form_id(); ?>"></td>';
echo "<td><img src='http://www.samleisure.co.uk/wordpress/wp-content/plugins/wp-e-commerce/themes/lsotheme/variationimgs/$row[image]' /></td>";

$thisRowCols++;
}
if($thisRowCols < $cols){
//we need to add empty columns to finish filling the row
while($thisRowCols < $cols){
echo '<td></td>';
$thisRowCols++;
}
echo '</tr>';
}
?>
[/code]

P.S. the "& #37 ; part should be a "%" (some encoding issue on this forum)
Copy linkTweet thisAlerts:
@AravonaauthorNov 24.2010 — I put it in and got:

Warning: Invalid argument supplied for foreach() in /usr/home/customers/www.site.co.uk/www/wordpress/wp-content/plugins/wp-e-commerce/themes/lsotheme/single_product.php on line 580
Copy linkTweet thisAlerts:
@criterion9Nov 24.2010 — What do you have now? Did you remember to include your query above the code?
Copy linkTweet thisAlerts:
@criterion9Nov 24.2010 — Just saw a logic error:
[code=php]
if($thisRowCols < $cols){
//we need to add empty columns to finish filling the row
while($thisRowCols < $cols){
echo '<td></td>';
$thisRowCols++;
}
echo '</tr>';
}
[/code]

should be:
[code=php]
//we need to add empty columns to finish filling the row
while($thisRowCols < $cols){
echo '<td></td>';
$thisRowCols++;
}
echo '</tr>';
[/code]
Copy linkTweet thisAlerts:
@AravonaauthorNov 24.2010 — [code=php]$query = "select image from wp_wpsc_variation_values where id = '" .wpsc_the_variation_id(). "'";
//don't forget to include your query here
echo "<table><tbody><tr>";
$i = 0;
$cols = 3;
$thisRowCols = 0;
foreach($nums as $num){ //forgot to switch this out for your code
$i++;
if($i &#37; $cols == 0){
echo '</tr><tr>';
$thisRowCols = 0;
}
echo '<td>
<input type="radio" value="<?php echo wpsc_the_variation_id(); ?>" class="wpsc_select_variation" name="variation[<?php echo wpsc_vargrp_id(); ?>]" id="<?php echo wpsc_vargrp_form_id(); ?>"></td>';
echo "<td><img src='http://www.samleisure.co.uk/wordpress/wp-content/plugins/wp-e-commerce/themes/lsotheme/variationimgs/$row[image]' /></td>";

$thisRowCols++;
}
//we need to add empty columns to finish filling the row
while($thisRowCols < $cols){
echo '<td></td>';
$thisRowCols++;
}
echo '</tr>';
}
echo "</tr></tbody></table>";[/code]


Still getting the same error. Sorry I've never used foreach before.
Copy linkTweet thisAlerts:
@criterion9Nov 24.2010 — [code=php]
$query = "select image from wp_wpsc_variation_values where variation_id = '1'";
$result = mysql_query($query); //you forgot this part
[/code]


Also you need to switch out my foreach line for your while.
[code=php]
while($row = mysql_fetch_array($result)) {
[/code]
Copy linkTweet thisAlerts:
@AravonaauthorNov 24.2010 — No errors now...

but the site looks like this :

The site
Copy linkTweet thisAlerts:
@criterion9Nov 24.2010 — What code are you using?
Copy linkTweet thisAlerts:
@AravonaauthorNov 24.2010 — [code=php]$query = "select image from wp_wpsc_variation_values where id = '" .wpsc_the_variation_id(). "'";
$result = mysql_query($query);
//don't forget to include your query here
echo "<table><tbody><tr>";
$i = 0;
$cols = 3;
$thisRowCols = 0;
while($row = mysql_fetch_array($result)) {
//forgot to switch this out for your code
$i++;
if($i % $cols == 0){
echo '</tr><tr>';
$thisRowCols = 0;
}
echo '<td>
<input type="radio" value="<?php echo wpsc_the_variation_id(); ?>" class="wpsc_select_variation" name="variation[<?php echo wpsc_vargrp_id(); ?>]" id="<?php echo wpsc_vargrp_form_id(); ?>"></td>';
echo "<td><img src='http://www.samleisure.co.uk/wordpress/wp-content/plugins/wp-e-commerce/themes/lsotheme/variationimgs/$row[image]' /></td>";

$thisRowCols++;
}
//we need to add empty columns to finish filling the row
while($thisRowCols < $cols){
echo '<td></td>';
$thisRowCols++;
}
echo '</tr>';
}
echo "</tr></tbody></table>";[/code]

Thats what I put in.
Copy linkTweet thisAlerts:
@criterion9Nov 24.2010 — Is this within another loop or something?
Copy linkTweet thisAlerts:
@AravonaauthorNov 24.2010 — Its within the while of the variations.

[code=php]<?php while (wpsc_have_variations()) : wpsc_the_variation(); ?>
<?php if (wpsc_vargrp_id() == '3') {
?>
<td>
<input type="radio" value="<?php echo wpsc_the_variation_id(); ?>" class='wpsc_select_variation' name="variation[<?php echo wpsc_vargrp_id(); ?>]" id="<?php echo wpsc_vargrp_form_id(); ?>"><?php echo wpsc_the_variation_name(); ?></td>
<?php }
else {
?>
<!--<option value="<?php echo wpsc_the_variation_id(); ?>" <?php echo wpsc_the_variation_out_of_stock(); ?><?php echo wpsc_the_variation_name(); ?></option>-->

<?php /*$query = "select image from wp_wpsc_variation_values where id = '" .wpsc_the_variation_id(). "'";
$result = mysql_query($query);
$count = 1;
while($count <8) {
while($row = mysql_fetch_array($result)) {

if($count == 6) {
echo "<tr>";
}
echo '<td>
<input type="radio" value="<?php echo wpsc_the_variation_id(); ?>" class="wpsc_select_variation" name="variation[<?php echo wpsc_vargrp_id(); ?>]" id="<?php echo wpsc_vargrp_form_id(); ?>"></td>';
echo "<td><img src='http://www.samleisure.co.uk/wordpress/wp-content/plugins/wp-e-commerce/themes/lsotheme/variationimgs/$row[image]' /></td>";
if($count == 5) {
echo "</tr>";
}
}
$count++;
}
}*/
$query = "select image from wp_wpsc_variation_values where id = '" .wpsc_the_variation_id(). "'";
$result = mysql_query($query);
//don't forget to include your query here
echo "<table><tbody><tr>";
$i = 0;
$cols = 3;
$thisRowCols = 0;
while($row = mysql_fetch_array($result)) {
//forgot to switch this out for your code
$i++;
if($i &#37; $cols == 0){
echo '</tr><tr>';
$thisRowCols = 0;
}
echo '<td>
<input type="radio" value="<?php echo wpsc_the_variation_id(); ?>" class="wpsc_select_variation" name="variation[<?php echo wpsc_vargrp_id(); ?>]" id="<?php echo wpsc_vargrp_form_id(); ?>"></td>';
echo "<td><img src='http://www.samleisure.co.uk/wordpress/wp-content/plugins/wp-e-commerce/themes/lsotheme/variationimgs/$row[image]' /></td>";

$thisRowCols++;
}
//we need to add empty columns to finish filling the row
while($thisRowCols < $cols){
echo '<td></td>';
$thisRowCols++;
}
echo '</tr>';
}
echo "</tr></tbody></table>";
?>

<?php
endwhile; ?>[/code]
Copy linkTweet thisAlerts:
@criterion9Nov 24.2010 — That makes more sense now...

while($row = mysql_fetch_array($result)) {
[/quote]

should be changed into your

while (wpsc_have_variations())
[/quote]
Copy linkTweet thisAlerts:
@AravonaauthorNov 24.2010 — Changing to 'while (wpsc_have_variations()) ' just leaves the page hanging :S
Copy linkTweet thisAlerts:
@criterion9Nov 24.2010 — Take a look at what my code did....if you need me to explain specific parts I'll try to do so. The code I posted at first (though untested) was to illustrate how to determine when to place the end/beginning of a row when building a table with an undetermined number of elements. The loop that is important is the external while loop in your code since the internal loop you had is just used to retrieve a single row and append a few cells to the table. Does that make sense?

Psuedocode:
<i>
</i>//start table element
//loop through variations
//pull the variation
// this doesn't need to be a loop...only needs the $row = mysql_fetch_array($result);
//spit out the contents
//check if we need to start a new row
//end loop
//finish out empty cells if needed
//complete table element
Copy linkTweet thisAlerts:
@AravonaauthorNov 24.2010 — It makes sense, but I don't see how to put it in the plugin code.

The Variations is code which just comes with the wordpress plugin I'm using.
Copy linkTweet thisAlerts:
@criterion9Nov 24.2010 — [code=php]
<?php
//start table element and set variables
echo "<table><tbody><tr>";
$i = 0;
$cols = 3;
$thisRowCols = 0;

while (wpsc_have_variations()) {//loop through variations
wpsc_the_variation();//pull the current variation

$query = "select image from wp_wpsc_variation_values where id = '" .wpsc_the_variation_id(). "'";
$result = mysql_query($query);// this doesn't need to be a loop...only needs the $row = mysql_fetch_array($result);
$row = mysql_fetch_array($result)

//increment our counter
$i++;

//check if we need to start a new row
if($i % $cols == 0){ //this should be the "percent" modulus symbol (is the current cell divisible evenly by our specified columns?)
echo '</tr><tr>'; //end the current row and start a new one
$thisRowCols = 0; //start the row/cell count over
}

//spit out the table cells
echo '<td><input type="radio" value="'.wpsc_the_variation_id().'" class="wpsc_select_variation" name="variation['.wpsc_vargrp_id().']" id="'.wpsc_vargrp_form_id().'"></td>';
echo "<td><img src='http://www.samleisure.co.uk/wordpress/wp-content/plugins/wp-e-commerce/themes/lsotheme/variationimgs/$row[image]' /></td>";

$thisRowCols++; //increment the number of grouped cells we have added to this row
} //end loop

//finish out empty cells if needed
while($thisRowCols < $cols){
echo '<td>&nbsp;</td><td>&nbsp;</td>'; //create an empty cell (in our case we need 2 each to match what we've put out above)
$thisRowCols++;
}

//complete table element
echo "</tr></tbody></table>";
?>
[/code]


Does that make more sense with the comments?
Copy linkTweet thisAlerts:
@AravonaauthorNov 25.2010 — Yes That does make a lot more sense.

However, it just throws up errors.

I gave in and changed the page layout instead.
Copy linkTweet thisAlerts:
@criterion9Nov 25.2010 — If you told us what the errors were we might be able to help you fix them...
×

Success!

Help @Aravona spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...