loop error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

loop error

Post 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;
 
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: loop error

Post by requinix »

Code: Select all

if ($fields[$b] = 'id')
if ($fields[$b] = 'category')
if ($fields[$b] = 'price')
= is for assignment, == is for comparison.
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: loop error

Post by michaelk46 »

Cool... Thanks Man
Post Reply