Jump to content


Photo

Need Assistance with A Complex While Loop Statement


  • Please log in to reply
2 replies to this topic

#1 mbait

mbait
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 01 March 2006 - 02:30 AM

Hello. I'm sort of new to PHP, but I'm in need of assistance with a WHILE loop statement.

This is what I'm trying to do.

Visitors who are interested in downloading a digital product from me, must first create a member account. A product link for that particular download is automatically placed in their Download Management area once the new account is created.

I'm wanting the PHP script to also place product links for other digital products I label as "Free" in their Download Management area too.

Here's what I have.

$result = mysql_query("SELECT * FROM products WHERE itemStatus='free' ORDER BY 'added' DESC") or DIE("Could not Select from products");
while ($data = mysql_fetch_assoc($result)) {

$itemId = $data['itemId'];
$itemName = $data['itemName'];

$result1 = mysql_query("SELECT * FROM filename WHERE itemId='$itemId' LIMIT 1") or DIE(mysql_error());
$data1 = mysql_fetch_assoc($result1);

if (mysql_num_rows($result1) > 0)
{
$itemId = $data1['itemId'];
$fileId = $data1['fileId'];
$resell = $data1['resell'];
if ($resell == 'NO') {
$type = 0;
}
else {
$type = 1;
}
}

$result2 = mysql_query("INSERT INTO downloads SET custId='$custId', orders='$orders', itemId='$itemId', fileId='$fileId', prod_fileName='$itemName', download_maxdays='$download_maxdays', download_maxcount='$download_maxcount', datedl='$datedl', ipaddr='$ipaddr', type='$type'") or DIE(mysql_error());
$downId = mysql_insert_id();
}


The above does work, but there is one problem. If the digital product they initially created their account for is "Free", the above While statement places a second entry for it in their Download Management. Is there anyway of saying...

If product X already exists for the Customer in the Downloads Table, then skip this row, returning to the top of the While loop looking for any other "Free" files to add.

Can someone help? Hope I explained this well enough.

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 01 March 2006 - 05:51 PM

If the original download they selected is passed to this script, you could alter your first query to something like this:
$result = mysql_query("SELECT * FROM products WHERE itemStatus='free' AND `itemId` != '$_POST[itemId]' ORDER BY 'added' DESC") or DIE("Could not Select from products");


#3 mbait

mbait
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 02 March 2006 - 12:18 AM

Hello. Actually after sleeping on it last night, I did come up with the very solution your suggesting.

I actually assigned a unique varName to the original product link being inserted and altered the line above the While statement to say:

$result = mysql_query("SELECT * FROM products WHERE itemStatus = 'free' and itemID != '$prodId' ORDER BY 'added' DESC") or DIE("Could not Select from Free products");

The above works and hopefully anyone else coming across a similar situation can use this to their advantage.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users