Jump to content

What is wrong with my loop?


zoreli

Recommended Posts

Hi! I am trying to make this loop with hours

and hopefully someone here will help me to sort this out.

 

 

 

I have 2 tables, colors and product colors.

 

Here are table structure:

 

colors:

 

colorid

colorname

colorphoto

 

Products_colors:

 

id

productid

colorid

 

What I am trying to do?

 

Some products may have 0,or 1 or more colors.

 

On my edit product page, I have a link colors, where in a popup window

I would like to do this:

 

To find if there are any colors added already in table products_colors.

 

If there are any colors added, I want to display them and next to the color

checkbox should be checked.

 

Rest of the colors should be added bellow (I am pooling them from table colors)

and checkboxes next to these colors should be unchecked.

 

 

 

If there are no records for that product in table products_colors, I would like to show all colors from table colors

with a checkbox (unchecked) next to the color photo.

 

 

How can I do this?

 

 

 

Bellow is my code which doesnt work correct (show only one color checked, even somewhere

I have 2 or 3 colors.) If there are no records in table products_colors, I get no colors.

 

 

 

<?php

include 'includes/config.php';

include 'includes/openDb.php';

$productid = $_GET['productid'];

 

 

?>

<form action="addProductColors.php" method="post">

<input type="hidden" name="productid" value="<?php echo $productid;?>">

 

<?php

 

$query="SELECT * FROM products_colors where productid = $productid ";

$result=mysql_query($query);

$num=mysql_numrows($result);

$a=0;

 

$query1="SELECT * FROM colors";

$result1=mysql_query($query1);

$num1=mysql_numrows($result1);

$b=0;

 

 

while ($a < $num) {

$productid=mysql_result($result,$a,"productid");

$colorid=mysql_result($result,$a,"colorid");

 

while ($b < $num1) {

$colorid1=mysql_result($result1,$b,"colorid");

$colorphoto1=mysql_result($result1,$b,"colorphoto");

$colorname1=mysql_result($result1,$b,"colorname");

 

 

 

echo "<img src='backOfficeImages/$colorphoto1' alt='$colorname1'>";

if ($colorid == $colorid1) {

echo "<input type='checkbox' name='$colorid' value='$colorid1' checked>";

echo "<br>";

 

}else{

echo "<input type='checkbox' name='$colorid' value='$colorid1'>";

echo "<br>";

}

 

$b++;

}

 

$a++;

}

 

 

 

 

echo "<input type='submit' value='Add - Remove Color(s)'></form>";

?>

 

 

Regards, Zoreli

Link to comment
https://forums.phpfreaks.com/topic/49994-what-is-wrong-with-my-loop/
Share on other sites

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.