Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

for loop problem 1

Status
Not open for further replies.

Hozzer

Technical User
Jul 30, 2009
20
CA
I have what looks to be a very simple problem but I can't seem to see the issue

$q = "SELECT * FROM subscriptions WHERE userID = '".$_SESSION['userid']."'";
$result = $database->query($q);
/* Error occurred, return given name by default */
$num_rows = mysql_numrows($result);
if(!$result || ($num_rows < 0)){
echo "Error displaying info";
return;
}
if($num_rows == 0){
echo "You currently have no promotions running, see available <a href=\"promotions.php\">promotions</a> here.";
return;
}
for($i=0; $i<$num_rows; $i++){
$promoPk = mysql_result($result,$i,"subPK");
$user = mysql_result($result,$i,"userID");
$promoCode = mysql_result($result,$i,"promoCode");
}echo $num_rows;

Now, when I echo num_rows the value is 2 but when I echo promoCode there is only one value when there should be 2...
Why would this be?
 
Because you are overwriting the value for $promoCode in subsequent runs of the loop.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Vancunita is right. You'll want to read each value in each row into a separate address to prevent overwriting. You can load the values into an array.

Code:
for($i=0; $i<$num_rows; $i++)
{
      $promoPk[$i] = mysql_result($result,$i,"subPK");
      $user[$i]  = mysql_result($result,$i,"userID");
      $promoCode[$i]  = mysql_result($result,$i,"promoCode");  
}
print_r($promoCode);  'This will display an easily readable variable structure

or load all values into a single multi-dimensional array

Code:
for($i=0; $i<$num_rows; $i++)
{
      $promos[$i]['PK']   = mysql_result($result,$i,"subPK");
      $promos[$i]['user'] = mysql_result($result,$i,"userID");
      $promos[$i]['code'] = mysql_result($result,$i,"promoCode");  
}
print_r($promos);  'This will display an easily readable variable structure
-Geates
 
Just another quick question, now that I have the array populated is there a way to loop through another query with the values stored in promoCode?
 
You mean generate queries based on the Promo codes?
Code:
foreach ($promos as $promo_code){

$query="SELECT *FROM mytable WHERE fieldname=$promo_code";
$result=mysql_query($query);
\\do whatever with the returned values. 
}


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
do you mean like this:

foreach($promos as $promo)
{
print $promo['PK'];
print $promo['user'];
print $promo['code'];

$q = "SELECT * FROM table WHERE promoCode='".$promo['code']."' and promoPK='".$promo['PK']."' and userID='".$promo['user']."';";
}

-Geates
 
I actually want to combine the results of two for loops, see below

foreach($promoPk as $promo_Pk){
echo $promo_Pk;
}
foreach ($promoCode as $promo_Code){
$qVendors = "SELECT coName,coType,packType FROM vendors WHERE pk = '$promo_Code'";
$result2 = $database->query($qVendors);
/* Error occurred, return given name by default */
$num_rows = mysql_numrows($result2);
if(!$result2 || ($num_rows < 0)){
echo "You have not subscribed to any promotions.";
return;
}
if($num_rows == 0){
return;
}
/* Display table contents */
for($i=0; $i<$num_rows; $i++){
$coName = mysql_result($result2,$i,"coName");
$coType = mysql_result($result2,$i,"coType");
$packType = mysql_result($result2,$i,"packType");
echo "<tr><td>$coName</td><td>$coType</td><td>$packType</td><td>$promo_Pk-$promo_Code</td><td><a href=\"/login/remove-promotions.php?pc=$promoCode\">&nbsp;&nbsp;&nbsp;Remove</a></td></tr>\n";
}
}

The last for loop is only displaying the last iteration of the loop though, I'm a little bit of a newb to for loops so I'm a little confused. When I echo $promo_Pk from inside the foreach loop it displays as it should but not when I echo from inside the last for loop...
 
You are ending your foreach loop for the promoPk code befoe doing anything else.
So it just echo's out a list of promo codes that are in promopk and then ends.

So it really does nothing with the rest of the loops you are using.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks Vacunita,
I've actually mad a little progress with nesting the loops. Insead of running two seperate loops I nested the "query" loop inside the first "$promoPk as $promo_Pk" loop.

That being said, I then switch the nested foreach to just a for loop and get this error:
Parse error: parse error, expecting `';'' in....

It works if I leave the nested loop as a foreach loop but doubles my results see below

foreach($promoPk as $promo_Pk){
for($promoCode as $promo_Code){
$qVendors = "SELECT coName,coType,packType FROM vendors WHERE pk = '$promo_Code'";
$result2 = $database->query($qVendors);
/* Error occurred, return given name by default */
$num_rows = mysql_numrows($result2);
if(!$result2 || ($num_rows < 0)){
echo "You have not subscribed to any promotions.";
return;
}
if($num_rows == 0){
return;
}
/* Display table contents */
for($i=0; $i<$num_rows; $i++){
$coName = mysql_result($result2,$i,"coName");
$coType = mysql_result($result2,$i,"coType");
$packType = mysql_result($result2,$i,"packType");
echo "<tr><td>$coName</td><td>$coType</td><td>$packType</td><td>$promo_Pk-$promo_Code</td><td><a href=\"/login/remove-promotions.php?pc=$promoCode\">&nbsp;&nbsp;&nbsp;Remove</a></td></tr>\n";
}
}
}
 
I think your parsed error is caused by incorrectly using the second for..loop. You are using it like a foreach.

Code:
for($promoCode as $promo_Code)
should be
Code:
for[b]each[/b]($promoCode as $promo_Code)

At the end of the code, you are trying to print out the object, $promoCode. $promoCode is the iterated array in
Code:
for[b]each[/b]($promoCode as $promo_Code)
. You'll want to use the value of the iteratation, $promo_Code.

Code:
echo "<tr><td>$coName</td><td>$coType</td><td>$packType</td><td>$promo_Pk-$promo_Code</td><td><a href=\"/login/remove-promotions.php?[b]pc=$promo_Code[/b]\">&nbsp;&nbsp;&nbsp;Remove</a></td></tr>\n";

-Geates

I'm not sure if this is the solution you are looking for or a simple typo when posting your code. Can you display your object structure [print_r($promoCode);]so I have a better idea of what you are trying to accomplish.
 
Thanks for your response Geates, I did print_r($promoCode); and here is the result
Array ( [0] => 5 [1] => 7 )

The code below gives me the correct data but twice...
print_r($promoCode);
foreach($promoPk as $promo_Pk){
foreach($promoCode as $promo_Code){
$qVendors = "SELECT coName,coType,packType FROM vendors WHERE pk = '$promo_Code'";
$result2 = $database->query($qVendors);
/* Error occurred, return given name by default */
$num_rows = mysql_numrows($result2);
if(!$result2 || ($num_rows < 0)){
echo "You have not subscribed to any promotions.";
return;
}
if($num_rows == 0){
return;
}
/* Display table contents */
for($i=0; $i<$num_rows; $i++){
$coName = mysql_result($result2,$i,"coName");
$coType = mysql_result($result2,$i,"coType");
$packType = mysql_result($result2,$i,"packType");
echo "<tr><td>$coName</td><td>$coType</td><td>$packType</td><td>$promo_Pk-$promo_Code</td><td><a href=\"/login/remove-promotions.php?pc=$promo_Code\">&nbsp;&nbsp;&nbsp;Remove</a></td></tr>\n";
}
}
}

 
If this helps I print_r($promoPk) as well
Array ( [0] => 5 [1] => 7 )

Array ( [0] => 31 [1] => 33 )
 
Hozzer, I'm lost as to what you trying to accomplish. When you say you get the correct results twice, what exactly do you mean. Remember, you are nesting loops.

Code:
foreach($promoPk as $promo_Pk)
   foreach($promoCode as $promo_Code)
This will iterate each element of $promoCode during each iteration of $promoPK.

If this is your data:

Code:
$promoCode: Array ( [0] => 5 [1] => 7 )
$promoPK:   Array ( [0] => 31 [1] => 33 )

then your code produces expected results. The following exemplifies the iterations

1st iteration of first loop:
promo_PK = 31
1st iteration of second loop:
promo_Code = 5
2nd iteration of second loop:
promo_Code = 7

2nd iteration of first loop:
promo_PK = 33
1st iteration of second loop:
promo_Code = 5
2nd iteration of second loop:
promo_Code = 7


-Geates
 
Sorry Geates,
My desired result on page would be
31-5
33-7

 
then all you need to do is

Code:
'Capture your data
for($i=0; $i<$num_rows; $i++)
{
      $arrPromos[$i]['PK']   = mysql_result($result,$i,"subPK");
      $arrPromos[$i]['user'] = mysql_result($result,$i,"userID");
      $arrPromos[$i]['code'] = mysql_result($result,$i,"promoCode");
 }

'Spit it out
foreach($arrPromos as $objPromo)
{
   $qVendors = "SELECT coName, coType, packType FROM vendors WHERE pk = '[b]".$objPromo['code']."[/b]'";
   r2 = $database->query($qVendors);

   // Error occurred, return given name by default
   $num_rows = mysql_numrows($result2);
   if(!$result2 || ($num_rows < 0))
   {
      echo "You have not subscribed to any promotions.";
      return;
   }

   if($num_rows == 0)
      return;

   /* Display table contents */
   for($i=0; $i<$num_rows; $i++)
   {
       $coName   = mysql_result($result2,$i,"coName");
       $coType   = mysql_result($result2,$i,"coType");
       $packType = mysql_result($result2,$i,"packType");

       echo "<tr><td>$coName</td><td>$coType</td><td>$packType</td>".
            "<td>[b]".$objPromo['PK']." - ".$objPromo['code']."[/b]</td>".
            "<td><a href=\"/login/remove-promotions.php?pc=[b]".$objPromo['code']."[/b]\">".
            "&nbsp;&nbsp;&nbsp;Remove</a></td></tr>\n";
   }
}

-Geates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top