Jump to content

Need Assistance with A Complex While Loop Statement


mbait

Recommended Posts

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.
Link to comment
Share on other sites

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

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.