Jump to content

Mr Chris

Members
  • Posts

    336
  • Joined

  • Last visited

    Never

Everything posted by Mr Chris

  1. Thanks again, but still have some issues if you could kindly still help Here's what I want to do: MYSQL table: colors id color So it can have lots of records in it ie: id = 1 color = black id = 2 colour = red Now what I want to do is basically query that table to get all the records out of the database and stick them in an array: Now this queries the database, but does not get the colour names out of it? $row = mysql_fetch_assoc(mysql_query("SELECT name FROM colours")); for ($i=0; $i<=count($row); $i++) { print "Number $i = {$row[$i]}"; } So it should echo: Number 0 = black Number 1 = red etc... Thanks
  2. Thanks, but sorry maybe I did not explain myself well. Basically the colours" Black", "White", "Green", "Purple" all exist in a MYSQL database (I don't want any values hard coded into the array) so I simply want to call them out of the database and put them in an array then run a 'for' statement to echo them to the page. How would I do that? Thanks
  3. Hello, Quick question i've been learning about arrays using php, but not with php & mysql. I was wondering if anyone could help me write the below to query a database for colours and put the results it finds in an array? IE instead of Using PHP where I manually enter the colours like so: $colours = array ("Black", "White", "Green", "Purple"); for ($i = 0; $i < count($colours); $i++) { echo "These are the Colours ".$i." is ".$colours[$i]."<br />"; } I put the results of this query select * from colors into the $colours array and they are outputted? Thanks
  4. Hello, I'm trying to get together a regular expression to remove http:// if it exists in a url string: <?php $web_url = "http://www.wewebbbywebsiteshere.com"; $website = preg_replace('/^(http¦ftp¦news)s?:\/+/i', '', $web_url); echo $website; ?> Anyone help as the above does not seem to work? Thanks
  5. Hello All, Wondering if anyone can Kindly help. I have several html files like so provided to me by a client: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>This is the title</title> </head> <body> <table width="400" border="0"> <tr> <td><h1>This is the title</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tincidunt tristique felis, a lobortis quam iaculis vitae. Donec lobortis mattis purus ac accumsan. Cras eget risus sed nunc bibendum rutrum. Praesent luctus, tellus at congue tincidunt, leo lacus aliquet purus, in pretium massa lectus ut neque. Phasellus id tortor turpis. Sed viverra dapibus elit placerat dictum.</p> </td> </tr> </table> </body> </html> Now what I want to do is drop this file in a folder then read each file and insert the data from: - inbetween the <h1></h1> tags - inbetween the <p></p> tags Now i've got the script working so it opens the files in a folder and reads them, but I don't know how to ask php to grab the contents inbetween these two tags (<h1></h1 & <p></p>) and place them in a MYSQL database table. Can anyone help? Thanks <?php /* Define and Open the Directory */ $path = '/home/************************/'; $dir = opendir($path); /* Loop through the directory using a while loop if $dir exists */ while (false !== ($file = readdir($dir))) { /* Read the files in the directory: one.txt, two.txt, three.txt */ $the_text = file_get_contents($path.$file); /* This is where I am Stuck /* Now need to Grab the contents inbetween <h1></h1> and assign to $headline /* And between the <p></p> tag and assign to $headline and $the_text /* But dont know how to do it /* Insert into DB*/ $SQL = "INSERT INTO testing_text (headline,the_text) VALUES ('$headline','$the_text')"; $QUERY = mysql_query($SQL); } ?>
  6. Hi All, Wondering if anyone can help. I have three image paths saved into a database ($image1, $image2, $image3) and a big long text string ($the_text) which can contain anything from 800 around 1500 words. Now what I want to do is wrap each image around $the_text. IE: - $image1 a one third down the page - $image2 two thirds down the page - $image3 outputted Last third down the page All depending on how many characters are in the text. Now the best i've come up with is outputting all three images before the text is called out like so: if($image1) { echo '<img class="float:left;" src="'.$image1.'" alt="" />'; } if($image2) { echo '<img class="float:right;margin-top:10px;" src="'.$image2.'" alt="" />'; } if($image3) { echo '<img class="float:left;margin-top:10px;" src="'.$image1.'" alt="" />'; } echo ' <p class="features-text">'.str_replace("\n","</p>\r\n\t\t\t<p class=\"the-text\">",$the_text).'</p> '; But this is far from ideal. Anyone help me achieve the above? Thanks
  7. Thanks Guys, One problem with that though the next month and year need to be created through variables with values assigned to then, which is where I was getting it wrong e.g: <?php $the_month = 09; //Set as August $the_year = 1999; //Set as 1999 ?> So I was hoping to do something like this: <?php $the_month = 09; //Set as August $the_year = 1999; //Set as 1999 echo $lastMonth = date("".$the_month."", strtotime("-1 month")); //Would be 08 echo "<br />"; echo $nextMonth = date("".$the_month."", strtotime("+1 month")); //Would be 10 echo "<br />"; echo $lastYear = date("".$the_year."", strtotime("-1 year")); //Would be 1998 echo "<br />"; echo $nextYear = date("".$the_year."", strtotime("+1 year")); //Would be 2000 ?> Thanks
  8. Hi I'm hoping someone can advise me please. Say I have a month and a year set to variables (no day) called via $_GET from a URL string /?year=2008&month=12 $year = $_GET['year']; // 2008 $month = $_GET['month']; // EG (12) So = December How can I write a html link dynamically depending on the values of $_GET which goes back a month and also goes forward a month with years also changing depending on the month of the year? So for example on my page my link for next month (in this instance) would be: <a href=2009/01>Next Month</a> and the previous month <a href=2008/11>Previous Month</a> Thanks in advance
  9. Hello, Wondering if anyone can help me with a query. Basically I have a list of last_updated dates in my table 2009-12-06 09:40:00 2009-12-04 09:42:00 2009-12-06 09:44:00 And I want to say select all of these records where last_updated is not in the last 2 days from the current date and time? SELECT last_updated from tbl WHERE last_updated BETWEEN CURRENT_DATE AND CURRENT_DATE - INTERVAL 2 DAY Anyone help? Thanks
  10. Hi All, I have a query for getting some values for a column out of a mysql database. Basically if the value is 1 'Yes' is ticked and if the value is 0 'No' is ticked. Apart from making the values checked or not checked. Now this works fine: <?php $subsections = mysql_query("SELECT * FROM file WHERE websheet = 0"); while ($subsection = mysql_fetch_array($subsections)) { echo ' <span> '.$subsection['description'].'<br /> <label for="'.$subsection['description'].'"> <input type="radio" class="checkbox" name="subsection['.$int.']" value="1"'; if (in_array($subsection['section_id'] == 1)) { echo ' checked="checked"'; } echo ' /> Yes <input type="radio" class="checkbox" name="subsection['.$int.']" value="1"'; if (in_array($subsection['section_id'] == 0)) { echo ' checked="checked"'; } echo ' /> No </label></span> '; if ($ml10 == 4) { $ml10 = 1; } else { $ml10++; } $int++; } // end while subsectionRows echo '</fieldset></div>'; //} // end while sectionRows ?> Now the Only problem with this is these part if (in_array($subsection['section_id'] == 1)) { echo ' checked="checked"'; } echo ' /> Yes if (in_array($subsection['section_id'] == 0)) { echo ' checked="checked"'; } echo ' /> No Basically depending if it's a Yes or no I want the checkbox to be checked, but I get this error: Warning: Wrong parameter count for in_array() in /home/********/public_html/cms-testing/admin/options/edit.php on line 146 Can anyone kindly help? Thanks
  11. Hello, Say I wanted to check a record in my database before submitting some data to that database via a form I thought i'd do it like so: $duplicates = mysql_num_rows(mysql_query("SELECT name FROM table WHERE fullname = $full_name")); if ($duplicates > 0) { $msg[] = "Error already exisits"; } However I get the message: mysql_num_rows(): supplied argument is not a valid MySQL result resource I thought that would work? Any idea as to why thanks?
  12. Hi All, I have a wierd problem with a count query: <?php $SQL = mysql_query("SELECT sum(pictures_taken) as no_pics FROM event_data WHERE $the_query AND event_publication = '6' AND unix_timestamp(event_date) < unix_timestamp(NOW()) AND DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= event_date"); $row = mysql_fetch_array($SQL); echo "".$row['no_pics']."\n"; ?> However, sometimes I get a value returned, but sometimes no value is returned. It seems to have most difficulty when the Value returned from the count is 0. Can anyone help?
  13. My Bad - column count did not match!
  14. Cheers Guys, I'll investigate further
  15. Hi, I'm trying to do a Query with a Union where I want to print the number of rows $tc contains. Can anyone see what's wrong with this: $tc = @mysql_num_rows(mysql_query("(SELECT * FROM tbl1 WHERE id = $id) UNION (SELECT * FROM tbl2 WHERE id = $id)")); print $tc;
  16. Hi, Have a very silly question. If I want to query a distinct name out of mysql database i'd use: Select DISTINCT name from names However, if I wanted the id of that distinct name also outputted BEFORE the name I thought i'd use Select DISTINCT id, name from names But that does no longer return distinct only records. Can anyone help? Thanks
  17. Hi, Scratching my head a little here... :-( I have a while loop which contains a query. If I run the query in MYSQL I get two results returned, but if I run it in php I get 1 result returned and its like my while loop is not working. Anyone see anything obvious below as to why the code does not loop - or if there is some kind of issue with the query that would stop this looping: Thanks $sql_one = " SELECT * FROM orders, names WHERE names.order_code =$order_code AND orders.order_code =$order_code ORDER BY orders.id"; $result_one = mysql_query($sql_one) or die(mysql_error()); $row = mysql_fetch_array($result_one); $your_name = $row['your_name']; $your_email = $row['your_email']; //Send email $message = "Dear ".$your_name."\n\nYou have now confirmed your order:\n\n"; $n=1; while ($row = mysql_fetch_assoc($result_one)) { $item_name = $row['item_name']; $amount = $row['amount']; $quantity = $row['quantity']; $total_price = $row['total_product_price']; $message .= "Product: ".$item_name."\n"; $message .= "Product Price: ".$amount."\n"; $message .= "Quantity: ".$quantity."\n"; $message .= "Total Product Price: ".$total_price."\n\n"; $n++; }
  18. Hello, I have a query whereby I want to check if an IP address has a record in the database that has been added within the last 10 minutes: However as you will see from the above I ran this query at 2009-07-24 12:44:00 which is more than 10 minutes after 2009-07-24 12:22:47 and a result is returned - why would that be?
  19. Thanks, that sounds ideal but how would I save all my data into a session with my for loop for amount, quantity and item_name: for ($i = 0; $i < count($_POST['amount']); $i++) { echo "{$_POST['item_name'][$i]}<br />"; echo "{$_POST['amount'][$i]}<br />"; echo "{$_POST['quantity'][$i]}<br />"; } and then output it for amount, quantity and item_name:? for ($i = 0; $i < count($_POST['amount']); $i++) { $message .= "{$_POST['item_name'][$i]}\n"; $message .= "{$_POST['amount'][$i]}\n"; $message .= "<p>{$_POST['quantity'][$i]}\n\n"; }
  20. Hello, I have a webpage and I echo data to that webpage via $_POST values sent from the previous page, which I echo on the page using a for loop which works nicely. <?php //***************************************** //Get the POST data SUBMITTED via the previous page and print it //***************************************** if ($_POST['Submit']) { for ($i = 0; $i < count($_POST['amount']); $i++) { echo "{$_POST['item_name'][$i]}<br />"; echo "{$_POST['amount'][$i]}<br />"; echo "{$_POST['quantity'][$i]}<br />"; } } //***************************************** //If Submit Button ON THIS PAGE is hit send an email //***************************************** if ($_POST['Submit_two']) { $message = "Here are your details:\n\n"; //This bit is the problem bit - THESE values are now lost as Submit_2 has been pressed?? for ($i = 0; $i < count($_POST['amount']); $i++) { $message .= "{$_POST['item_name'][$i]}\n"; $message .= "{$_POST['amount'][$i]}\n"; $message .= "<p>{$_POST['quantity'][$i]}\n\n"; } /////////////////////////////////////////////////////// $message .= "Many Thanks"; $headers = "From: My Name<[email protected]>\r\n"; mail($your_email, "This is the headline", $message, $headers); } ?> <form action="" method="post" name="form" id="form"> <button name="Submit_two" type="submit" value="Submit_two">Submit_two</button> </form> Now as you will see on this page I also want to send an email, but ONLY when the Submit_two buttton is hit on this page, and in that email I want to grab those $POST values that I echo initially sent from the first page and send them in an email, but when I press Submit_two to do this the initial values are lost? So is there any way I can somehow keep them in memory to send in my email?
  21. Hello, I have a function i've written to output the hidden fields that paypal requires to process a payment like so using a foreach function Paypal($Database) { if($this->NumberOfItems() > 0){ $total = 0; $output = '<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_cart"> <input type="hidden" name="upload" value="1"> <input type="hidden" name="business" value="??????????????"> <input type="hidden" name="currency_code" value="GBP"> '; $i = 1; foreach ($this->Cart as $ProductID => $Qty){ $result = $Database->query('SELECT * FROM shop WHERE id = '.$ProductID); $row = $result->fetch(); $price = sprintf('£%.2f', $row['price']); $priceUnit = sprintf($row['price']); $rowTotal = sprintf('£%.2f', $row['price'] * $Qty); $total += $row['price'] * $Qty; $output .= <<<ROW <input type="hidden" name="item_name_{$i}" value="{$row['description']}"> <input type="hidden" name="amount_{$i}" value="{$priceUnit}"> <input type="hidden" name="quantity_{$i}" value="{$Qty}"> ROW; $i++; } $total = sprintf('£%.2f', $total); $output .= <<<END <input class="paybtn" type="submit" value="Pay"> </form> END; return $output; } } Now this works great, but at the same time as running this function and foreach statement to output the hidden fields I'd also like to INSERT the data into a database table row-by row. Is this possible? The table would just be a simple one like so: items -------- id (auto inc) description amount quantity But i'm not sure how to include this in the function so it does this?
  22. Hello I have my query to select a distinct column item named product_grouping (which is an INT in the db) as well as the other columns id and price which are not distinct. SELECT DISTINCT product_grouping, id, price FROM tbl WHERE product_grouping !=3 ORDER BY rand( ) LIMIT 4 But it produces these results: product_grouping | id | price --------------------------------- 8 |5 | 9.99 1 |4 |9.99 2 |6 |9.99 1 |2 |9.99 But I ask for a distinct product grouping yet I get two 1 results returned?
  23. Hello All, I'm trying to put a script on my site which will count the number of times an article has been viewed. Here's the tbl structure: CREATE TABLE `page_counter` ( `count_id` int(11) NOT NULL auto_increment, `count` int(20) NOT NULL default '1' `article_id` int(11) default NULL, `ip_address` varchar(30) default NULL, PRIMARY KEY (`count_id`) ) ; And the php <? $sql="select count from page_counter where article_id='".$article_id."'"; $count=0; $count_result=mysql_query($count_sql); if(mysql_num_rows($count_result)) { $count_row=mysql_fetch_row($count_result); $count=$count_row[0]; echo "This page has been accessed by:".$count." Visitors<br>"; } $count=$count+1; $update_sql="update page_counter set count=".$count." where article_id='".$article_id."'"; if(!mysql_query($update_sql)) { echo mysql_error(); } ?> Now this should work great but before I implement it, is there anyway I could make sure BOTS are not recorded in my counter, maybe by checking the user agent or is there a better way? Thanks
  24. Hi, I'm a bit stuck and would like some help please if someone would be so kind. I have a variable which contains a long string of text. Now I have a piece of php code which finds the middle of that code and I can output some text there or an image (like below) <? //Example of what comes out of database and is assigned for the variable $text $text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi quam lorem, posuere et scelerisque eu, dictum eget odio."; $len = (strlen($text) >> 1) - 2; // insert at sentence boundary echo preg_replace( "/(?<=.{{$len}}[.!?])\s/", "<br />[ QUOTE WOULD GO HERE HALF WAY IN ]<br />", $text, 1); ?> Now this works fine, what i'd really like to do is half way in output a quote box i've made, but ONLY if the number of characters in the string exceeds a set amount in this case 200, otherwise it won't appear: if ($quote) && strlen($text) > 200 { echo ' <div style="float:right; display:inline;"> <div class="top_quote_box"></div> <div class="middle_quote_box">'. $quote .'</div> <div class="end_quote_box"></div> </div> '; } But I can't seem to plug the two in together, i've tried several ways but it throws up syntax errors left right and centre. Could anyone help me with this, or maybe advise a better way of doing this? Also while i'm here, would there also be a way to bolden the first paragraph only in the $text at the same time?
  25. Hello, I have a counter on my page which increments a column in my database every time a story on my site is viewed: $result = mysql_query("SELECT * FROM stories WHERE story_id=".$id); $row = mysql_fetch_array($result); $i = $row['my_count']; $i++; $query = "UPDATE stories SET read_count = '$i' WHERE story_id=".$id; $result = mysql_query($query); Is there anyway I could make it a bit better to not allow for things like a user sitting there and hitting refresh 15 times, and bots visiting the page, things like that really? Thanks
×
×
  • 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.