Jump to content

John_A

Members
  • Posts

    70
  • Joined

  • Last visited

Everything posted by John_A

  1. I've got a mailing list (uses DadaMail) which provides a perl script (subscribe_email.pl) so that I can pass subscribers to it thorugh a PHP script. Info here: http://dadamailproject.com/support/documentation-5_0_1/COOKBOOK-subscriptions.pod.html#command_line_utility___subscribe_email_pl The trouble is I can get it working when variables are passed to it via URL query strings, but when I try to hard-code variables into it it stops working. I can't see any reason for this, it's got me really confused. This works: - <?php // example usage: - // http://www.domain.com/ds1.php?sendto=user@domain.com&title=Mr&forename=John&surname=Doe&company=None&country=UK if (isset($_GET['sendto'])) { $email = $_GET['sendto']; } if (isset($_GET['title'])) { $title = preg_replace("/[^A-Za-z ]/", "", $_GET['title']); } if (isset($_GET['forename'])) { $forename = preg_replace("/[^A-Za-z\-. ]/", "", $_GET['forename']); } if (isset($_GET['surname'])) { $surname = preg_replace("/[^A-Za-z\-. ]/", "", $_GET['surname']); } if (isset($_GET['company'])) { $company = preg_replace("/[^A-Za-z()\-.& ]/", "", $_GET['company']); $company = str_replace('&', 'and', $company); } if (isset($_GET['country'])) { $country = preg_replace("/[^A-Za-z\-.& ]/", "", $_GET['country']); $country = str_replace('&', 'and', $country); } if (isset($email)) { $exec_command = 'perl ./subscribe_email.pl --list mylist --email '. $email; if (isset($title)) $exec_command .= ' --fields title '. $title; if (isset($forename)) $exec_command .= ' --fields forename '. $forename; if (isset($surname)) $exec_command .= ' --fields surname '. $surname; if (isset($company)) $exec_command .= ' --fields company '. $company; if (isset($country)) $exec_command .= ' --fields country '. $country; exec(escapeshellcmd($exec_command)); } else { echo 'Nothing to do'; } ?> The mail address and all the parameters are passed to the subscribe_email.pl script. But, this doesn't work: - <?php $subscriber_email = 'user@domain.com'; $subscriber_title = 'Mr'; $subscriber_forename = 'John'; $subscriber_surname = 'Doe'; $subscriber_company = 'None'; $subscriber_country = 'UK'; if (isset($subscriber_email)) { $email = escapeshellcmd($subscriber_email); } if (isset($subscriber_title)) { $title = preg_replace("/[^A-Za-z ]/", "", $subscriber_title); } if (isset($subscriber_forename)) { $forename = preg_replace("/[^A-Za-z\-. ]/", "", $subscriber_forename); } if (isset($subscriber_surname)) { $surname = preg_replace("/[^A-Za-z\-. ]/", "", $subscriber_surname); } if (isset($subscriber_company)) { $company = preg_replace("/[^A-Za-z()\-. ]/", "", $subscriber_company); $company = str_replace('&', 'and', $company); } if (isset($subscriber_country)) { $country = preg_replace("/[^A-Za-z\-. ]/", "", $subscriber_country); $country = str_replace('&', 'and', $country); } if (isset($email)) { $exec_command = 'perl ./subscribe_email.pl --list mylist --email '. $email; if (isset($title)) $exec_command .= ' --fields title '. $title; if (isset($forename)) $exec_command .= ' --fields forename '. $forename; if (isset($surname)) $exec_command .= ' --fields surname '. $surname; if (isset($company)) $exec_command .= ' --fields company '. $company; if (isset($country)) $exec_command .= ' --fields country '. $country; exec(escapeshellcmd($exec_command)); } else { echo 'Nothing to do'; } ?> In the second example, only the list and email parameters are passed, all the others are lost (or ignored). All that's different between the two is the way the variables are inititally set, I can't for the life of me figure out why the second one isn't working . Even stranger, I tried testing with just: - <?php exec("perl ./subscribe_email.pl --list mylist --email user@domain.com --fields title Mr --fields forename John--fields surname Doe --fields company None --fields country UK") ?> and this doesn't work fully either (again just the list and email parameters are passed). Unfortunately, it's the second method I need to use as it's tagged on to the end of a contact form processing script which initiates the subscription if a checkbox is left ticked...and all variables will be coming from this form script. Any help greatly appreciated!
  2. Thanks, I'll give that a try and stick with it unless anyone has a more efficient method! The source is a DB from a 3rd party application, which I don't want to interfere with. I actually edited it with a JOIN but had to keep applying the edit every upgrade, this new approach is as a 'plug-in' to basically inject what I need into the existing array.
  3. I have one array, called for example $MainImages : - Array ( [0] => Array ( [widgetID] => 143 [image] => image_a.jpg ) [1] => Array ( [widgetID] => 147 [image] => image_b.jpg ) [2] => Array ( [widgetID] => 73 [image] => image_c.jpg ) ) and a second array, we'll call it $widgets: - Array ( [0] => Array ( [widgetID] => 147 [seo_title] => An excellent class B Widget [name] => Widget B [categoryID] => 1 ) [1] => Array ( [widgetID] => 73 [seo_title] => An excellent class C Widget [name] => Widget C [categoryID] => 1 ) [2] => Array ( [widgetID] => 143 [seo_title] => An excellent class A Widget [name] => Widget A [categoryID] => 1 ) ) I want to merge the value from the [][image] key from the first array into a new [][image] key / value in the second, using a shared value as a reference...so the desired result array is: - Array ( [0] => Array ( [widgetID] => 147 [seo_title] => An excellent class B Widget [name] => Widget B [categoryID] => 1 [image] => image_b.jpg ) [1] => Array ( [widgetID] => 73 [seo_title] => An excellent class C Widget [name] => Widget C [categoryID] => 1 [image] => image_c.jpg ) [2] => Array ( [widgetID] => 143 [seo_title] => An excellent class A Widget [name] => Widget A [categoryID] => 1 [image] => image_a.jpg ) ) Obviously the order is different and so they need to be merged based on the widgetID values. Also, the same widgetID may be in each array more than once. I've looked at a lot of help but can't find anything to do exactly what I need....any help would be greatly appreciated!
  4. Excellent, just what I needed. Thanks
  5. Many thanks! What's the best way to then remove the offending -[digit] from the string (sorry, I should have included this in my original question)?
  6. I've got a string which is usually abc-def-0123 but it sometimes has an additional dash and then a single digit (e.g. ghi-jkl-4567-1). It's always the same pattern and length, (three letters, a dash, three letters, a dash, 4 digits then the possibility of a dash followed by a single digit). What's the best way to test if the string ends in a dash followed by a single digit? Any help much appreciated!
  7. Thanks for your help! I ended up with this which works great: - function showproductinfo($thisproduct){ echo 'Product #' . $thisproduct['product_id'] . ' is a ' . $thisproduct['colour'] . $thisproduct['title'] . ', code: ' . $thisproduct['code']; } showproductinfo($all_products[1]);
  8. It's not perfect, but it works. All I'm asking is, is it better to declare the global array variable within the function, or to pass the array to the function?
  9. Every time? I'm calling the function 50 times or more on the same page.
  10. Thanks both for your help! I did this: - function showproductinfo($thisproduct){ global $all_products; echo 'Product #' . $thisproduct . ' is a ' . $all_products[$thisproduct]['colour'] . $all_products[$thisproduct]['title'] . ', code: ' . $all_products[$thisproduct]['code']; } showproductinfo(1); Which seems to work. Are there any downsides to this?
  11. I thought this would be fairly straightforward but for some reason isn't working. I have an array built from some MySQL data: - $all_products[1] = array( "title"=>"widget", "product_code"=>"wid-123", "colour"=>"blue"); $all_products[2] = array( "title"=>"thingy", "product_code"=>"thi-345", "colour"=>"red"); This works fine when used like: - echo 'Product #1 is a ' . $all_products[1]['colour'] . $all_products[1]['title'] . ', code: ' . $all_products[1]['code']; Gives: "Product #1 is a blue widget, code: wid-123" However, if I do this: - function showproductinfo($thisproduct) { echo 'Product #' . $thisproduct . ' is a ' . $all_products[$thisproduct]['colour'] . $all_products[$thisproduct]['title'] . ', code: ' . $all_products[$thisproduct]['code']; } showproductinfo(1); It gives nothing Can I use a variable passed via a function as the array key in this way? If so, how? And if not, what's the best way to do what I, trying to do?
  12. Thanks again Scot, you have been most helpful and it is much appreciated
  13. That's excellent Scot, thanks a lot. It does indeed do exactly what I wanted Just a few questions:- 1) The "parent" and "child" labels I gave in my example are known values, which I admit wasn't clear by my choice of using "parent" and "child"! Would it be more efficient to use these when looking for the keys / values, rather than checking every possibiilty (it's part of a fairly large array). 2) Is it easily changed to look for "This" or "That" (or an array of strings to check for)?
  14. Lets sat I have a multidimensional array. call it $my_array.... Print_r($my_array) gives: - Array ( [parent] => Array ( [child] => Array ( [0] => Array ( [itemID] => 3 [name] => This Item [lowlimit] => 50 [highlimit] => 0 ) [1] => Array ( [itemID] => 6 [name] => That Item [lowlimit] => 50 [highlimit] => 0 ) ) ) ) It's the child items I want to deal with. It won't always have both items, it may have only 1 or more. I want to look for any child item who's [name] key includes the string "This", and if found remove that whole child item (in this case $my_array -> parent -> child -> 0) and then fix the indexing again so they start at 0 and increment OK. The trouble is, I don't know where to start? Any help much appreciated!
  15. Hi, I need a function to return the next x working days (Mon - Fri). So for 7 from today it would return: - Monday 8th March Tuesday 9th March Wednesday 10th March Thursday 11th March Friday 12th March Monday 15th March Tuesday 16th March But also with the ability to pass to it days to be excluded (for example Christmas / Boxing day and New Year's Day, as well as any custom public holidays). I've done something similar before but it ended up being rather long, I'm looking to keep this as short and simple as possible....any ideas?
  16. You are of course correct! Shortly after my last post here I realised that what had started out as a MySQL question had turned into a PHP one, so I cross-posted at http://www.phpfreaks.com/forums/index.php/topic,273536.msg1292207.html. I marked that thread as solved, and hadn't done this one - sorry!
  17. This ouputs:- which is correct indicates that a class is stored in $instructions[0] rather than an array :-\ I have no idea what that means....or how I managed to do it that way. The question now is, is it workable as it is? I need to check if an array exists for each document type, if it does output a heading etc. then list the relevant docs....i.e. can I still loop through each array?
  18. First - this is a cross-posting, if this is wrong I apologise! What started out as a MySQL query question (original post http://www.phpfreaks.com/forums/index.php/topic,273323.0.html) has become a PHP coding question.... I have 2 tables - docs and doc_relationships: - +-------------+--------------+---Table: docs-----+-----------+---------------------+---------+ | document_id | doctype | location | title | thumbnail | added | visible | +-------------+--------------+-----------+-------+-----------+---------------------+---------+ | 1 | instructions | pdf/a.pdf | a | img/a.jpg | 2001-04-12 20:16:58 | Y | +-------------+--------------+-----------+-------+-----------+---------------------+---------+ | 2 | pressrelease | pdf/b.pdf | b | img/b.jpg | 2002-04-09 20:18:33 | Y | +-------------+--------------+-----------+-------+-----------+---------------------+---------+ | 3 | newsletter | pdf/c.pdf | c | img/c.jpg | 2003-05-21 11:18:54 | Y | +-------------+--------------+-----------+-------+-----------+---------------------+---------+ | 4 | instructions | pdf/x.pdf | x | img/x.jpg | 2004-01-22 19:13:21 | Y | +-------------+--------------+-----------+-------+-----------+---------------------+---------+ | 5 | pressrelease | pdf/y.pdf | y | img/y.jpg | 2005-02-17 17:11:17 | Y | +-------------+--------------+-----------+-------+-----------+---------------------+---------+ | 6 | instructions | pdf/z.pdf | z | img/z.jpg | 2006-07-02 12:15:58 | Y | +-------------+--------------+-----------+-------+-----------+---------------------+---------+ | 7 | pressrelease | pdf/p.pdf | p | img/p.jpg | 2002-03-01 18:31:26 | Y | +-------------+--------------+-----------+-------+-----------+---------------------+---------+ +-------Table: doc_relationships-----+ | rel_id | document_id | product_id | +--------+--------------+------------+ | 1 | 1 | 4 | +--------+--------------+------------+ | 2 | 2 | 3 | +--------+--------------+------------+ | 3 | 4 | 3 | +--------+--------------+------------+ | 4 | 6 | 2 | +--------+--------------+------------+ | 5 | 7 | 3 | +--------+--------------+------------+ Each row in the doc_relationships table ties one document to a product_id (product_ids are held in another table and will be hardcoded), the above example gives: - doc 1: instructions, product 4 doc 2: pressrelease, product 3 doc 3: newsletter doc 4: instructions, product 3 doc 5: pressrelease doc 6: instructions, product 2 doc 7: pressrelease, product 3 Each product can have any number of Instructions, Press Releases and Newsletters related to it. Press Releases and Newsletters can be related to one or more product. Instruction manuals are usually related to one product only. So far I have three pages - newsletters, pressreleases and instructions, which simply list all the matching documents, using this query: - $query="SELECT * FROM docs WHERE doctype='newsletter' AND visible='Y' ORDER BY added DESC, document_id DESC"; The problem is I also want to list any relevant documents on product pages, under headings referrring to doc types, so for example on product 3's page: - ----------------------------------------------------------------------------------------- Product #3 - Super Widget Desription - this is a remarkable widget, you'll wonder how you ever managed without one! Instructions: (link to doc 4) Press Releases: - (link to doc 2) (link to doc 7) ----------------------------------------------------------------------------------------- The query I'm using for the product pages is as follows: - $query="SELECT * FROM docs, doc_relationships WHERE doc_relationships.document_id=docs.document_id AND doc_relationships.product_id='3' AND visible='Y' ORDER BY docs.added DESC, docs.document_id DESC"; I could add another condition to this query for "doctype" and do it 3 times, once for each doctype, but ideally what I'd like to do on a product page is create 3 arrays (newsletters, pressreleases & instructions) containing the details of each for that product (if any). So here's what I have so far: - // connect to database and issue the query mysql_connect($server,$dbusername,$dbpassword); @mysql_select_db($db_name) or die( "Unable to select database"); $query="SELECT * FROM docs, doc_relationships WHERE doc_relationships.document_id=docs.document_id AND doc_relationships.product_id='3' AND visible='Y' ORDER BY docs.added DESC, docs.document_id DESC"; $all_documents=mysql_query($query); $total_docs=mysql_num_rows($all_documents); mysql_close(); if ($total_docs != 0) { while ($sql = mysql_fetch_object($all_documents)) { if ($sql -> doctype=='newsletter'){ echo 'document #' . $sql -> document_id . ' is a newsletter<br />'; // create the newsletters array if it doesnt exist if (!is_array($newsletters)) $newsletters= array(); // add to newsletters array; array_push($newsletters, $sql); } else if ($sql -> doctype=='pressrelease'){ echo 'document #' . $sql -> document_id . ' is a pressrelease<br />'; // create the pressreleases array if it doesnt exist if (!is_array($pressreleases)) $pressreleases= array(); // add to pressreleases array; array_push($pressreleases, $sql); } else if ($sql -> doctype=='instructions'){ echo 'document #' . $sql -> document_id . ' is instructions<br />'; // create the instructions array if it doesnt exist if (!is_array($instructions)) $instructions array(); // add to instructions array; // not quite sure how! array_push($instructions, $sql); } } } print_r($instructions); As you can see, I added some test echos and this works as expected. The result of the final print_r($instructions) command is: - The problem is I can't seem to extract the data from this array individually, for example I thought: - echo $instructions[0]['document_id']; would echo '4' but it doesn't (and actually stops the page output dead). So: - 1) Am I inserting results into the arrays correctly? 2) How do I access individual document info from each array? 3) How do I (for each array) loop through all documents?
  19. Thanks for the replies! It dawned on me that each document can only be one document type (newsletter, pressrelease or instructions) and so I added the doctype as a column in the documents table, so now have: - +-------------+--------------+---Table: docs-----+-----------+---------------------+---------+ | document_id | doctype | location | title | thumbnail | added | visible | +-------------+--------------+-----------+-------+-----------+---------------------+---------+ | 1 | instructions | pdf/a.pdf | a | img/a.jpg | 2001-04-12 20:16:58 | Y | +-------------+--------------+-----------+-------+-----------+---------------------+---------+ | 2 | pressrelease | pdf/b.pdf | b | img/b.jpg | 2002-04-09 20:18:33 | Y | +-------------+--------------+-----------+-------+-----------+---------------------+---------+ | 3 | newsletter | pdf/c.pdf | c | img/c.jpg | 2003-05-21 11:18:54 | Y | +-------------+--------------+-----------+-------+-----------+---------------------+---------+ | 4 | instructions | pdf/x.pdf | x | img/x.jpg | 2004-01-22 19:13:21 | Y | +-------------+--------------+-----------+-------+-----------+---------------------+---------+ | 5 | pressrelease | pdf/y.pdf | y | img/y.jpg | 2005-02-17 17:11:17 | Y | +-------------+--------------+-----------+-------+-----------+---------------------+---------+ | 6 | instructions | pdf/z.pdf | z | img/z.jpg | 2006-07-02 12:15:58 | Y | +-------------+--------------+-----------+-------+-----------+---------------------+---------+ | 7 | pressrelease | pdf/p.pdf | p | img/p.jpg | 2002-03-01 18:31:26 | Y | +-------------+--------------+-----------+-------+-----------+---------------------+---------+ +-------Table: doc_relationships-----+ | rel_id | document_id | product_id | +--------+--------------+------------+ | 1 | 1 | 4 | +--------+--------------+------------+ | 2 | 2 | 3 | +--------+--------------+------------+ | 3 | 4 | 3 | +--------+--------------+------------+ | 4 | 6 | 2 | +--------+--------------+------------+ | 5 | 7 | 3 | +--------+--------------+------------+ Now the query for the three "list all of a certain doctype" pages is much easier, like: - $query="SELECT * FROM docs WHERE doctype='newsletter' AND visible='Y' ORDER BY added DESC, document_id DESC"; And the query for the product pages is a modified version of the oen from my first post: - $query="SELECT * FROM docs, doc_relationships WHERE doc_relationships.document_id=docs.document_id AND doc_relationships.product_id='3' AND visible='Y' ORDER BY docs.added DESC, docs.document_id DESC"; I could add another condition to this query for "doctype" and do it 3 times, once for each doctype, but ideally what I'd like to do on a product page is create 3 arrays (newsletters, pressreleases & instructions) containing the details of each for that product (if any). So here's what I have so far: - // connect to database and issue the query mysql_connect($server,$dbusername,$dbpassword); @mysql_select_db($db_name) or die( "Unable to select database"); $query="SELECT * FROM docs, doc_relationships WHERE doc_relationships.document_id=docs.document_id AND doc_relationships.product_id='3' AND visible='Y' ORDER BY docs.added DESC, docs.document_id DESC"; $all_documents=mysql_query($query); $total_docs=mysql_num_rows($all_documents); mysql_close(); if ($total_docs != 0) { while ($sql = mysql_fetch_object($all_documents)) { if ($sql -> doctype=='newsletter'){ echo 'document #' . $sql -> document_id . ' is a newsletter<br />'; // create the newsletters array if it doesnt exist if (!is_array($newsletters)) $newsletters= array(); // add to newsletters array; array_push($newsletters, $sql); } else if ($sql -> doctype=='pressrelease'){ echo 'document #' . $sql -> document_id . ' is a pressrelease<br />'; // create the pressreleases array if it doesnt exist if (!is_array($pressreleases)) $pressreleases= array(); // add to pressreleases array; array_push($pressreleases, $sql); } else if ($sql -> doctype=='instructions'){ echo 'document #' . $sql -> document_id . ' is instructions<br />'; // create the instructions array if it doesnt exist if (!is_array($instructions)) $instructions array(); // add to instructions array; // not quite sure how! array_push($instructions, $sql); } } } print_r($instructions); As you can see, I added some test echos and this works as expected. The result of the final print_r($instructions) command is: - The problem is I can't seem to extract the data from this array individually, for example I thought: - echo $instructions[0]['document_id']; would echo '4' but it doesn't (and actually stops the page output dead). So: - 1) Am I inserting results into the arrays correctly? 2) How do I access individual document info from each array? 3) How do I (for each array) loop through all documents?
  20. I need some help cross-referencing tables within a MySQL query in PHP. I currently have a database with 3 tables: - ------------------------------Table: docs-------------------------------------- | document_id | location | title | thumbnail | added |visible | ------------------------------------------------------------------------------- | 1 | pdf/a.pdf | a | img/a.jpg | 2001-04-12 20:16:58 | Y | | 2 | pdf/b.pdf | b | img/b.jpg | 2002-04-09 20:18:33 | Y | | 3 | pdf/c.pdf | c | img/c.jpg | 2003-05-21 11:18:54 | Y | | 4 | pdf/x.pdf | x | img/x.jpg | 2004-01-22 19:13:21 | Y | | 5 | pdf/y.pdf | y | img/y.jpg | 2005-02-17 17:11:17 | Y | | 6 | pdf/z.pdf | z | img/z.jpg | 2006-07-02 12:15:58 | Y | ------------------------------------------------------------------------------- --------Table: doctypes-------- | doctype_id | doctype_name | ------------------------------- | 1 | newsletter | | 2 | pressrelease | | 3 | instructions | ------------------------------- -----------Table: doc_relationships---------------- | rel_id | document_id | doctype_id | product_id | --------------------------------------------------- | 1 | 1 | 3 | 0 | | 2 | 1 | 0 | 4 | | 3 | 2 | 2 | 0 | | 4 | 2 | 0 | 3 | | 5 | 3 | 1 | 0 | | 6 | 4 | 3 | 0 | | 7 | 4 | 0 | 3 | | 8 | 5 | 2 | 3 | | 9 | 6 | 3 | 0 | | 9 | 6 | 0 | 2 | | 10 | 7 | 2 | 0 | | 11 | 7 | 0 | 3 | --------------------------------------------------- product_ids are held in another table and will be hardcoded for this purpose. Each row in the doc_relationships table ties one document to either a doctype_id or product_id, the above example gives: - doc 1: instructions, product 4 doc 2: pressrelease, product 3 doc 3: newsletter doc 4: instructions, product 3 doc 5: pressrelease doc 6: instructions, product 2 doc 7: pressrelease, product 3 Each product can have any number of Instructions, Press Releases and Newsletters related to it. Press Releases and Newsletters can be related to one or more product. Instruction manuals are usually related to one product only. So far I have three pages - newsletters, pressreleases and instructions, which simply list all the matching documents, using this query: - $query="SELECT * FROM docs, doc_relationships, doctypes WHERE doc_relationships.document_id=docs.document_id AND doc_relationships.doctype_id=doctypes.doctype_id AND visible='Y' AND doctype_name='newsletter' ORDER BY docs.added DESC, docs.document_id DESC"; This example returns all (in this case) newsletters in reverse chronoligical order. This works fine (although if there's a better way I'd be happy to change it) - my problem is I also want to list any relevant documents on product pages, under headings referrring to doc types, so for example on product 3's page: - ----------------------------------------------------------------------------------------- Product #3 - Super Widget Desription - this is a remarkable widget, you'll wonder how you ever managed without one! Instructions: (link to doc 4) Press Releases: - (link to doc 2) (link to doc 7) ----------------------------------------------------------------------------------------- So, I want a similar query to the first for each document type relevant to the product, i.e. I want to filter results further the by product_ID (product_ID will be hardcoded, as the doctype_name in my intital query is). I hope I've explained clearly enough! Any ideas? PHP 5.2.10 and MySQL 5.0.81.
  21. I have a bookings.txt file which has a list of dates each on a new line, for example: - 21/06/07 22/06/07 29/06/07 25/12/07 01/01/08 I also have a variable $event_dd_mm_yy which I want to remove from this file if found. I've managed to put the file contents into an array with: - $list = explode("\n",file_get_contents("../bookings.txt")); And I'm checking if the $event_dd_mm_yy is there using: - if (array_search($event_dd_mm_yy, $list) === FALSE) { echo 'Date not found in file.'; } else { // date found in file // insert code here to remove $event_dd_mm_yy from array // then update bookings.txt } Trouble is I don't know where to start in either removing the item from the array or re-writing the file! Any help much appreciated
×
×
  • 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.