Jump to content

Bhaal

Members
  • Posts

    60
  • Joined

  • Last visited

    Never

Everything posted by Bhaal

  1. Brilliant! I'd never have figured this out. Thank you SO MUCH!
  2. Double '&&' has the same results: it skips the first value pair. I checked the names of the text fields just to make sure - all of them are as follows: <li>Artist: <input type="text" size="25" name="artist[]" class="enter" > Song: <input type="text" size="45" name="song[]" class="enter" ></li> So why would the first set be skipped? Very strange.
  3. Thanks for the help! The query quoted above has the exact same results as the original: 25 records inserted with many blanks (when there should be only as many records as text boxes with values). Playing around with it yielded different results...sort of. This version inserts records for all the values filled in (posted) EXCEPT for the first set: if (count($_POST['artist']) > 0) { foreach ($_POST['artist'] as $key => $val) { $song = $_POST['song'][$key]; $artist = $val; if(!empty($val) & !empty($key)) { $q = "insert into comp_items (compitem_artist, compitem_song, CompID ) values ('$artist', '$song', '$lastID')"; mysql_query($q); } } } So if the user enters three sets of 'artist name', 'song name' only the last two are saved. If they enter 24 sets, only the last 23 are saved. But no blank records, so it's much closer. But having 'or' (as in " if(!empty($val) || !empty($key)) { " ) enters blank records. Darn - so close...kinda frustrating (nope: ignorance is not bliss).
  4. There are two tables: comp_main and comp_items. comp_main has an auto increment ID field: 'comp_id'; comp_items has a 'foreign key' field: 'CompID'. One-to-many: comp_main to comp_items based on that ID. I'm attempting to build a data entry form that allows the user to enter the comp_main info AND the comp_items info. Getting the info into comp_main is the easy part: $qa = "insert into comp_main set comp_name = '$_POST[comp_name]', comp_desc = '$_POST[comp_desc]' "; mysql_query($qa); The difficult part (for me) is the comp_items info. There are only four fields in the comp_items table: - an auto increment ID field; - artist name; - song name; - CompID (the 'foreign key' from comp_main). The form has 25 text boxes for 'artist name' and 25 text boxes for 'song name' (this is the way the client wants it, so they can enter 'artist name', 'song name' then tab to the next set and enter 'artist name', 'song name' on down the form - up to 25 times). I know the comp_items foreign key field (CompID) will be the last inserted ID from the first query: $lastID = mysql_insert_id(); So I'm trying to loop through the 25 sets of artist and song text boxes and only insert those that are filled in: if (count($_POST['artist']) > 0) { foreach ($_POST['artist'] as $key => $val) { $song = $_POST['song'][$key]; $artist = $val; $q = "insert into comp_items (compitem_artist, compitem_song, CompID ) values ('$artist', '$song', '$lastID')"; mysql_query($q); } } That loop does insert the artist name, song name and CompID correctly, but it also inserts multiple 'blank' record with $lastID as CompID. For instance, if the user enters two artist names and song names, there are 25 records entered into comp_items (instead of just 2) with 23 of those records having artist name and song name blank but with CompID as $lastID. It should only enter as many records as there are posts for $artist and $song. (Man I hope I'm explaining this accurately enough!) So, how can I loop through the form, grab the entered values and insert them into comp_items w/o any 'blank' records?
  5. Mark it done! Thank you Jay. Now how do I mark this 'solved'?
  6. Would you mind explaining further? Put 'it' in an array 'first'. Is 'it' $artName? Is 'first' before the print statement - but still inside the while? Or is 'first' before the eregi function? ('Cause I've tried all those: $artName = array(); )
  7. Scenario: table with artist names (and other fields); some names have "The" (as in 'The Dumb Koders'), others have "A" ('A Wilhelm Scream') but most are just a name ('Joe Smith'). I'm using eregi to take the 'The' and place it after the actual name - so it changes 'The Beatles' to 'Beatles, The' - for the purpose of sorting. Except now I don't know how to sort these results - since they are strings. So the sort() function doesn't work. The entire code: <? $sql = "select distinct artistName, song_file_name1, song_file_name2, song_file_name3, song_title1, song_title2, song_title3, desc, sync from indies where ((artistName like '%$srch%')) order by indies.artistName ASC"; $result_artist = mysql_query($sql) or die ($sql . mysql_error()); while ($row = mysql_fetch_array($result_artist)) { $artName = eregi ("^(The|An|A)+ (.*)$", $row['artistName'], $regs) ? $regs[2].", ".$regs[1] : $row['artistName']; print $artName . "<br /> "; } mysql_close(); ?> How can I 're-sort' $artName after running an eregi on it? As I understand it, $artName is not an array therefore sort() doesn't work. Is there a 'simple' way of resorting the eregi results before printing to the page? (Otherwise, the eregi is pretty much useless.) Thanks in advance.
  8. Got it. But of course now there's another problem: re-sorting the results. I'll ask about this in another thread.
  9. For me, it's more of a hate-hate relationship.
  10. This is just dumb 'cause it should be "easy". Got a field holding 'artist name' (in an array) so when looped it prints: Joe Smith The Nube Koders Henry Lee Lisa Jones The Idiots ... (etc.) When sorting, the record "The Nube Koders" gets put in the "T" section obviously. So I need to take the string "The" and place it after the actual name (with a comma) so it prints: Nube Koders, The Pretty sure eregi will work for this (eregi not ereg since some of the names are all caps). But nothing I try is working. After much consternation: $artName = eregi ("(.+),( the)", $row['artistName'], $regs) ? $regs[1].", ".$regs[2] : $row['artistName']; Doesn't work at all though. No surprise there. Any hints? Thanks...
  11. Thanks redarrow. I made one other change that seems to work - instead of: $newQty = $qty++; I'm using: $newQty = $qty + 1; ...which works in a testing environment... THANKS ALL! I truly appreciate it! (Mark this one: resolved)
  12. Code hasn't changed since I last posted it: if($_POST["submit_x"]) { $sql = "select * from cart where OrderID = '$PHPSESSID' "; $rql = mysql_query($sql) or die(mysql_error()); $rows = mysql_num_rows($rql); if($rows == '0') { $MyTotal = $_POST[itemPrice]; $q1 = "insert into cart set OrderID = '$PHPSESSID', ItemID = '$_POST[itemID]', ItemName = '$_POST[itemName]', ItemPrice = '$_POST[itemPrice]', ItemQty = '1', ItemType = '$_POST[itemType]', ItemTotal = '$MyTotal' "; mysql_query($q1) or die(mysql_error()); } else { while($aql = mysql_fetch_array($rql)) { $myItem = $aql[itemID]; $qty = $aql[itemQty]; $newQty = $qty++; $ItemID = $_POST[itemID]; if($myItem == $ItemID) { $q1 = "update cart set ItemQty = $newQty where OrderID = '$PHPSESSID' and ItemID = '$ItemID' "; $uq = mysql_query($q1); } } if(!$uq) { $MyTotal = $_POST[itemPrice]; $q1 = "insert into cart set OrderID = '$PHPSESSID', ItemID = '$_POST[itemID]', ItemName = '$_POST[itemName]', ItemPrice = '$_POST[itemPrice]', ItemQty = '1', ItemType = '$_POST[itemType]', ItemTotal = '$MyTotal' "; mysql_query($q1) or die(mysql_error()); } } }
  13. Well, I still don't understand why the update query portion of this code isn't firing. Any ideas about that?
  14. Alright sspoke - good advice, but much easier said than done, for a newbie like me at least. "...make a function or something in MySQL for that not PHP..." I don't think I've ever attempted anything like that - is that like a stored procedure in SQLServer? Does MySQL support functions on the database level??
  15. This is kind of working (thanks, roopurt18!): if($_POST["submit_x"]) { $sql = "select * from cart where OrderID = '$PHPSESSID' "; $rql = mysql_query($sql) or die(mysql_error()); $rows = mysql_num_rows($rql); if($rows == '0') { $MyTotal = $_POST[itemPrice]; $q1 = "insert into cart set OrderID = '$PHPSESSID', ItemID = '$_POST[itemID]', ItemName = '$_POST[itemName]', ItemPrice = '$_POST[itemPrice]', ItemQty = '1', ItemType = '$_POST[itemType]', ItemTotal = '$MyTotal' "; mysql_query($q1) or die(mysql_error()); } else { while($aql = mysql_fetch_array($rql)) { $myItem = $aql[itemID]; $qty = $aql[itemQty]; $newQty = $qty++; $ItemID = $_POST[itemID]; if($myItem == $ItemID) { $q1 = "update cart set ItemQty = $newQty where OrderID = '$PHPSESSID' and ItemID = '$ItemID' "; $uq = mysql_query($q1); } } if(!$uq) { $MyTotal = $_POST[itemPrice]; $q1 = "insert into cart set OrderID = '$PHPSESSID', ItemID = '$_POST[itemID]', ItemName = '$_POST[itemName]', ItemPrice = '$_POST[itemPrice]', ItemQty = '1', ItemType = '$_POST[itemType]', ItemTotal = '$MyTotal' "; mysql_query($q1) or die(mysql_error()); } } } It IS preventing duplicates (YEAH!!!). However - it's not updating the Quantity ($qty). The first part sees if there are records in the cart - if not, do a simple insert. If there are records, it loops through and tries to find dupes. If it finds dupes, it's supposed to do an update. If it doesn't find dupes it's supposed to do the insert. If the item exists, it's supposed to increment quantity by 1, hence the line: $qty = $aql[itemQty]; $newQty = $qty++; My guess is that it's not firing the update portion of the query at all. I can't see the flaw in the logic. Any clues? Thanks again - this is already a huge leap forward.
  16. I removed the single quotes around $_POST[itemID] in the line: $ItemID = $_POST[itemID]; - but the result is the sameas before: nothing is saved to the table.
  17. I have a simple "add to cart" function that simply adds a record to a "cart" table. But, I'd really like it to check and see if the item being added is already in the table. If it's not in the table, do an insert query; if it's already in the table, do an update query - and update the "quantity" field, by incrementing it by one. Original insert query: if($_POST["submit_x"]) { $MyTotal = $_POST[itemPrice]; $q1 = "insert into cart set OrderID = '$PHPSESSID', ItemID = '$_POST[itemID]', ItemName = '$_POST[itemName]', ItemPrice = '$_POST[itemPrice]', ItemQty = '1', ItemType = '$_POST[itemType]', ItemTotal = '$MyTotal' "; mysql_query($q1) or die(mysql_error()); } Please don't laugh at the following, but here's what I've been trying: if($_POST["submit_x"]) { //get current table contents $sql = "select * from cart where OrderID = '$PHPSESSID' "; $rql = mysql_query($sql) or die(mysql_error()); while($aql = mysql_fetch_array($rql)) { $myItem = $aql[itemID]; $qty = $aql[itemQty]; $newQty = $qty++; $ItemID = '$_POST[itemID]'; if($myItem == $ItemID){ $q1 = "update cart set ItemQty = $newQty where OrderID = '$PHPSESSID' and ItemID = '$ItemID' "; mysql_query($q1) or die(mysql_error()); } else { $MyTotal = $_POST[itemPrice]; $q1 = "insert into cart set OrderID = '$PHPSESSID', ItemID = '$_POST[itemID]', ItemName = '$_POST[itemName]', ItemPrice = '$_POST[itemPrice]', ItemQty = '1', ItemType = '$_POST[itemType]', ItemTotal = '$MyTotal' "; mysql_query($q1) or die(mysql_error()); } } } This obviously doesn't work at all. Any help is very much appreciated...
  18. Yeah...I know that javascript is client side and PHP is server side - hence the reason I'm posting this. I found a pretty nice MP3 player that can be 'fed' an MP3 by a javascript function. I'm using PHP to query a table, then telling the javascript which MP3 to play based on the results of that query. So... I run a simple select query, then populate a variable: $demoName = "'" . $a2[url_path] . $a2[demo_file_name] . "'"; Then the MP3 player is invoked by the following link: <a href='javascript:void(0);' onclick='javascript:loadFile($demoName);return false;'>Play</a> The function "loadFile" is pretty simple: function loadFile(fil) { thisMovie("mp3player").loadFile(fil); }; (The referenced function "thisMovie" just identifies the instance of the MP3 player.) What I would LOVE to happen is that each time this javascript is run, a counter field for that MP3 is increased by one, something like: $query = "UPDATE mp3s SET count=count+1 WHERE mp3_id=$id" I pretty much understand all that code I just don't know how to make them work "together". Is there a way to run that update query after the javascript function is run? Any help is greatly appreciated!
  19. Hey all, Here's the issue as concisely as I can make it. A band site sells MP3s that are downloaded once notification of payment is received. Buyers can add individual MP3s into a cart and/or add whole Albums (containing multiple MP3s) into a cart. Tables are setup similar to Categories/Products, except it's Albums/Mp3s (one table for Albums and another for MP3s; one-to-many: each Album holds multiple MP3s; an MP3 must have an Album record). All MP3s chosen are then zipped into a single archive file for downloading. It was kind of freaking me out that there are two levels of 'product' - since each item added to the cart is a single line item (due to pricing: purchasing an Album is cheaper than purchasing all the MP3s in that album separately). So, if user adds 2 MP3s and 1 Album, that's 3 line items. Found a script that takes care of the archiving. It's nice. Anyway, what I'd done is set a flag in the cart so it knows the difference between MP3s and Albums. At checkout, I run the following queries to gather the files and zip them: //create zip file and save name of file to orders_info table $filename = "dwn_" . $order_id . ".zip"; $test = new zip_file("zip_files/$filename"); $test->set_options(array('inmemory' => 0, 'recurse' => 0, 'storepaths' => 0)); //these are params for the archive script $qnew = "update orders_info set dwnfile = '$filename' where OrderID = '$order_id' "; mysql_query($qnew) or die(mysql_error()); //get the path and mp3s in shopping cart //first check the 'mp3' flag $q4 = "select ItemID from cart where ItemType = 'mp3' and OrderID = '$order_id' "; $r4 = mysql_query($q4) or die(mysql_error()); while($a4 = mysql_fetch_array($r4)) { $ItemID[] = $a4[itemID]; } if(count($ItemID) > '0') { while(list(, $value) = each($ItemID)) { $ItemInfo = explode("|", $value); $q5 = "select song_file_name, downloadURL from mp3s where rec_id = '$ItemInfo[0]' "; $r5 = mysql_query($q5) or die(mysql_error()); while($a5 = mysql_fetch_array($r5)) { $song = $a5[song_file_name]; $path = $a5[downloadURL]; $mydwn = $path . $song; $test->add_files($mydwn); } } } //then check the 'album' flag $q6 = "select ItemID from cart where ItemType = 'album' and OrderID = '$order_id' "; $r6 = mysql_query($q6) or die(mysql_error()); while($a6 = mysql_fetch_array($r6)) { $ItemID[] = $a6[itemID]; } if(count($ItemID) > '0') { while(list(, $value) = each($ItemID)) { $ItemInfo = explode("|", $value); $q7 = "select song_file_name, downloadURL from mp3s where AlbumID = '$ItemInfo[0]' "; $r7 = mysql_query($q7) or die(mysql_error()); while($a7 = mysql_fetch_array($r7)) { $song = $a7[song_file_name]; $path = $a7[downloadURL]; $mydwn = $path . $song; $test->add_files($mydwn); } } } $test->create_archive(); //create the zip file and save it This works - it actually does gather the MP3 files and zips them - yippee! However, I'm worried about a couple of things: 1. There's no error checking - not really sure how/what I should do about that 2. It's possible to add multiple instances of the same file to the cart (which is actually the 'add to cart' code, not this 'checkout' code...still, thought I'd mention it to see if anyone has ideas about this). I'm quite confident that there are much better ways of doing this but as a newbie, this is the solution I was able to cobble together. Any advice on how to improve/error check something like this? (Next step is sending an email with a temporary link to actually download the zip.) Thanks!!
  20. Wow - I think this is working! Thanks!
  21. Thanks for the help, jsladek. Unfortunately, deleting everything is not what I need.  I need to delete only those files that are found within a range, as explained in a previous message.
  22. Perhaps I should start over and ask in a semi-generic fashion. How does one delete (unlink) a 'range' of graphic files? THE SCENARIO: There is a mySQL table - let's call it MyTable. This table has a field called MyThumb.  This field holds the actual name of a graphic image.  (The actual graphic file is uploaded to the server and exists in a subdir called /thumbs.) I need to delete records from MyTable based on a range - and that range can be found by searching on a field called 'gallery'.  Therefore, I also need to delete the actual graphic image files that correspond with each of these found records. For example, this code will find a range of records: [code] $q1 = "select * from MyTable where gallery=$nid"; $r1 = mysql_query($q1) or die("Problem with the query: $q1<br>" . mysql_error()); [/code] It will also, therefore, find a 'range' of graphic image file names. If this found range (from the code above) contains 3 records, it will contain 3 values from the field MyThumb. These 3 values are the actual names of the graphics that have been uploaded to the server.  For example, these names could be: MyImage1.jpg, MyImage2.jpg and MyImage3.jpg. I know how to actually delete the records (easy enough) but how do I delete (unlink) the actual graphic files contained in this range?  How do I delete MyImage1.jpg, MyImage2.jpg and MyImage3.jpg from the server? I assume that an array of the image names would be used - but I just can't get it to work. I truly hope this makes the problem clearer.  Any help would be greatly appreciated. Thanks!
  23. I dunno.  Don't ask.  (Oh well, you already did.) What do you recommend?
  24. Well - each record in the table has a field called 'filename' which stores one and only one graphic file.  (So, if there are 10 records in the table, there are 10 graphics.) I'm pretty sure that "|" char in the the explode function is messing it up.  Is there a better approach to unlinking a bunch of graphic?
  25. "...storing the list of filenames..." What?  I don't quite get what that means.  I'm not storing a "list of filenames".  There are a bunch of files (thumbnail graphics, actually) that reside in a subdir called "thumbs" (hence the path in 'unlink("../thumbs/$v2")').  These files are copied to the server and the file name is stored in a field called "filename" from within a different form. The form I'm inquiring about deletes these files.  Since there are multiple files to delete (and multiple records - each record contains a thumbnail graphic), I thought that putting them in an array and deleting them with a 'while' loop would work.  But it's only deleting one file. Does that help?
×
×
  • 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.