Page 1 of 1

loop error

Posted: Sat Jan 23, 2010 3:03 pm
by michaelk46
I am having an issue with the second While Loop in the code below:

Here is the output of the $sql variable:
SELECT pages.idpages.categorypages.price, pages.idpages.categorypages.price FROM pages WHERE id ="1"

Here is what I want it to be:

SELECT pages.id, pages.category, pages.price FROM pages WHERE id ="1"

any ideas why it's looping three times on each pass?

Code: Select all

 
$tarray = array(1);
$fields = array('id', 'category', 'price');
$number = count($tarray);
$fnumber = count($fields);
$a = 0;
while ($a < $number)
    {
        $sql ='SELECT ';
        $b=0;
        while ($b < $fnumber)
            {
                if ($fields[$b] = 'id')
                    {
                        $sql .= 'pages.id';
                    }
                
                if ($fields[$b] = 'category')
                    {
                        $sql .= 'pages.category';
                    }
                
                if ($fields[$b] = 'price')
                    {
                        $sql .= 'pages.price';
                    }
                if ($b < --$fnumber)
                    {
                        $sql .= ', ';   
                    }
                else
                    {
                        $sql .= ' ';
                    }   
                ++$b;
            }
        $sql .= 'FROM pages WHERE id ="' . $tarray[$a] . '";';
        ++$a;  
    }
echo $sql;
 
 

Re: loop error

Posted: Sat Jan 23, 2010 3:46 pm
by requinix

Code: Select all

if ($fields[$b] = 'id')
if ($fields[$b] = 'category')
if ($fields[$b] = 'price')
= is for assignment, == is for comparison.

Re: loop error

Posted: Sat Jan 23, 2010 4:06 pm
by michaelk46
Cool... Thanks Man