Jump to content

Psycho

Moderators
  • Posts

    12,159
  • Joined

  • Last visited

  • Days Won

    130

Everything posted by Psycho

  1. Corrected code below. Note, I've split the data from the logic to make management much, much easier. Change your current script file to just this: <?php //include the data file include("craiglistURLS.php"); //Set limit for posts per url $post_limit = 60; //Get url for the selected state $urls = (in_array($_POST['state'], array_keys($urlList))) ? $urlList[$_POST['state']]: array(); //Get contents for each url foreach ($urls as $url) { $html = file_get_contents("$url/muc/"); //Extract link data preg_match_all('/<a href="([^"]+)">([^<]+)<\/a>[^<]*<font size="-1">([^"]+)<\/font>/s', $html, $posts, PREG_SET_ORDER); //Limit post resutls $posts = array_slice($posts, 0, $post_limit); //Display the results foreach ($posts as $post) { echo "<a href=\"{$post[1]}\" target=\"_blank\">{$post[2]}<font size=\"3\">{$post[3]}</font></a>\n"; print "<br /><br />\n"; } } ?> Create a separate file to store all the URLs. Call it something like craiglistURLS.php. Just make sure you reference it the same in the above script. <?php $urlList = array( "AL" => array("http://auburn.craigslist.org", "http://bham.craigslist.org"), "auburn" => array("http://auburn.craigslist.org"), "bham" => array("http://bham.craigslist.org"), "columbusga" => array("http://columbusga.craigslist.org"), "dothan" => array("http://dothan.craigslist.org"), "shoals" => array("http://shoals.craigslist.org"), "gadsden" => array("http://gadsden.craigslist.org"), "huntsville" => array("http://huntsville.craigslist.org"), "montgomery" => array("http://montgomery.craigslist.org"), "ALASKA" => array("http://alaska.craigslist.org"), "anchorage" => array("http://anchorage.craigslist.org"), "fairbanks" => array("http://fairbanks.craigslist.org"), "kenai" => array("http://kenai.craigslist.org"), "juneau" => array("http://juneau.craigslist.org"), "ARIZONA" => array("http://arizona.craigslist.org"), "flagstaff" => array("http://flagstaff.craigslist.org"), "mohave" => array("http://mohave.craigslist.org"), "phoenix" => array("http://phoenix.craigslist.org"), "prescott" => array("http://prescott.craigslist.org"), "showlow" => array("http://showlow.craigslist.org"), "sierravista" => array("http://sierravista.craigslist.org"), "tucson" => array("http://tucson.craigslist.org"), "yuma" => array("http://yuma.craigslist.org"), "ARKANSAS" => array("http://arkansas.craigslist.org"), "fayar" => array("http://fayar.craigslist.org"), "fortsmith" => array("http://fortsmith.craigslist.org"), "jonesboro" => array("http://jonesboro.craigslist.org"), "littlerock" => array("http://littlerock.craigslist.org"), "memphis" => array("http://memphis.craigslist.org"), "texarkana" => array("http://texarkana.craigslist.org"), "CALIFORNIA" => array("http://california.craigslist.org"), "sfbay" => array("http://sfbay.craigslist.org"), "bakersfield" => array("http://bakersfield.craigslist.org"), "chico" => array("http://chico.craigslist.org"), "fresno" => array("http://fresno.craigslist.org"), "goldcountry" => array("http://goldcountry.craigslist.org"), "hanford" => array("http://hanford.craigslist.org"), "humboldt" => array("http://humboldt.craigslist.org"), "imperial" => array("http://imperial.craigslist.org"), "inlandempire" => array("http://inlandempire.craigslist.org"), "merced" => array("http://merced.craigslist.org"), "modesto" => array("http://modesto.craigslist.org"), "monterey" => array("http://monterey.craigslist.org"), "orangecounty" => array("http://orangecounty.craigslist.org"), "palmsprings" => array("http://palmsprings.craigslist.org"), "redding" => array("http://redding.craigslist.org"), "reno" => array("http://reno.craigslist.org"), "sacremento" => array("http://sacremento.craigslist.org"), "sandiego" => array("http://sandiego.craigslist.org"), "slo" => array("http://slo.craigslist.org"), "santabarbara" => array("http://santabarbara.craigslist.org"), "santamaria" => array("http://santamaria.craigslist.org"), "siskiyou" => array("http://siskiyou.craigslist.org"), "stockton" => array("http://stockton.craigslist.org"), "susanville" => array("http://susanville.craigslist.org"), "ventura" => array("http://ventura.craigslist.org"), "visalia" => array("http://visalia.craigslist.org"), "yubasutter" => array("http://yubasutter.craigslist.org"), "COLORADO" => array("http://colorado.craigslist.org"), "boulder" => array("http://boulder.craigslist.org"), "cosprings" => array("http://cosprings.craigslist.org"), "denver" => array("http://denver.craigslist.org"), "eastco" => array("http://eastco.craigslist.org"), "fortcollins" => array("http://fortcollins.craigslist.org"), "rockies" => array("http://rockies.craigslist.org"), "pueblo" => array("http://pueblo.craigslist.org"), "westslope" => array("http://westslope.craigslist.org"), "CONNECTICUT" => array("http://connecticut.craigslist.org"), "newlondon" => array("http://newlondon.craigslist.org"), "hartford" => array("http://hartford.craigslist.org"), "newhaven" => array("http://newhaven.craigslist.org"), "nwct" => array("http://nwct.craigslist.org"), "newyork" => array("http://newyork.craigslist.org"), "District of Columbia" => array("http://washingtondc.craigslist.org"), "DELAWARE" => array("http://delaware.craigslist.org"), "FLORIDA" => array("http://florida.craigslist.org"), "daytona" => array("http://daytona.craigslist.org"), "keys" => array("http://keys.craigslist.org"), "fortmyers" => array("http://fortmyers.craigslist.org"), "gainesville" => array("http://gainesville.craigslist.org"), "cfl" => array("http://cfl.craigslist.org"), "jacksonville" => array("http://jacksonville.craigslist.org"), "lakeland" => array("http://lakeland.craigslist.org"), "lakecity" => array("http://lakecity.craigslist.org"), "ocala" => array("http://ocala.craigslist.org"), "okaloosa" => array("http://okaloosa.craigslist.org"), "orlando" => array("http://orlando.craigslist.org"), "panamacity" => array("http://panamacity.craigslist.org"), "pensacola" => array("http://pensacola.craigslist.org"), "sarasota" => array("http://sarasota.craigslist.org"), "miami" => array("http://miami.craigslist.org"), "spacecoast" => array("http://spacecoast.craigslist.org"), "staugustine" => array("http://staugustine.craigslist.org"), "tallahassee" => array("http://tallahassee.craigslist.org"), "tampa" => array("http://tampa.craigslist.org"), "treasure" => array("http://treasure.craigslist.org"), "GEORGIA" => array("http://georgia.craigslist.org"), "albanyga" => array("http://albanyga.craigslist.org"), "atlanta" => array("http://atlanta.craigslist.org"), "augusta" => array("http://augusta.craigslist.org"), "brunswick" => array("http://brunswick.craigslist.org"), "albanyga" => array("http://albanyga.craigslist.org"), "columbusga" => array("http://columbusga.craigslist.org"), "macon" => array("http://macon.craigslist.org"), "nwga" => array("http://nwga.craigslist.org"), "savannah" => array("http://savannah.craigslist.org"), "statesboro" => array("http://statesboro.craigslist.org"), "valdosta" => array("http://valdosta.craigslist.org"), "HAWAII" => array("http://honolulu.craigslist.org"), "IDAHO" => array("http://idaho.craigslist.org"), "boise" => array("http://boise.craigslist.org"), "eastidaho" => array("http://eastidaho.craigslist.org"), "lewiston" => array("http://lewiston.craigslist.org"), "pullman" => array("http://pullman.craigslist.org"), "spokane" => array("http://spokane.craigslist.org"), "twinfalls" => array("http://twinfalls.craigslist.org"), "ILLINOIS" => array("http://bn.craigslist.org", "http://chambana.craigslist.org", "http://chicago.craigslist.org" , "http://decatur.craigslist.org" , "http://lasalle.craigslist.org", "http://mattoon.craigslist.org", "http://peoria.craigslist.org", "http://quadcities.craigslist.org", "http://rockford.craigslist.org", "http://carbondale.craigslist.org" ), "bn" => array("http://bn.craigslist.org"), "chambana" => array("http://chambana.craigslist.org"), "chicago" => array("http://chicago.craigslist.org"), "decatur" => array("http://decatur.craigslist.org"), "lasalle" => array("http://lasalle.craigslist.org"), "mattoon" => array("http://mattoon.craigslist.org"), "peoria" => array("http://peoria.craigslist.org"), "quadcities" => array("http://quadcities.craigslist.org"), "rockford" => array("http://rockford.craigslist.org"), "carbondale" => array("http://carbondale.craigslist.org"), "springfieldil" => array("http://springfieldil.craigslist.org"), "stlouis" => array("http://stlouis.craigslist.org"), "quincy" => array("http://quincy.craigslist.org"), "INDIANA" => array("http://bn.craigslist.org", "http://bloomington.craigslist.org", "http://evansville.craigslist.org" , "http://fortwayne.craigslist.org" ,"http://indianapolis.craigslist.org", "http://kokomo.craigslist.org", "http://tippycanoe.craigslist.org", "http://muncie.craigslist.org", "http://richmondin.craigslist.org", "http://southbend.craigslist.org", "http://terrahaute.craigslist.org" ), "bloomington" => array("http://bloomington.craigslist.org"), "evansville" => array("http://evansville.craigslist.org"), "bloomington" => array("http://bloomington.craigslist.org"), "fortwayne" => array("http://fortwayne.craigslist.org"), "indianapolis" => array("http://indianapolis.craigslist.org"), "kokomo" => array("http://kokomo.craigslist.org"), "tippycanoe" => array("http://tippycanoe.craigslist.org"), "muncie" => array("http://muncie.craigslist.org"), "richmondin" => array("http://richmondin.craigslist.org"), "southbend" => array("http://southbend.craigslist.org"), "terrahaute" => array("http://terrahaute.craigslist.org"), "IOWA" => array("http://ames.craigslist.org", "http://bloomington.craigslist.org", "http://cedarrapids.craigslist.org" , "http://desmoines.craigslist.org" ,"http://dubuque.craigslist.org", "http://fortdodge.craigslist.org", "http://iowacity.craigslist.org", "http://masoncity.craigslist.org", "http://omaha.craigslist.org", "http://quadcities.craigslist.org", "http://siouxcity.craigslist.org", "http://ottumwa.craigslist.org", "http://waterloo.craigslist.org"), "ames" => array("http://ames.craigslist.org"), "cedarrapids" => array("http://cedarrapids.craigslist.org"), "desmoines" => array("http://desmoines.craigslist.org"), "dubuque" => array("http://dubuque.craigslist.org"), "fortdodge" => array("http://fortdodge.craigslist.org"), "iowacity" => array("http://iowacity.craigslist.org"), "masoncity" => array("http://masoncity.craigslist.org"), "omaha" => array("http://omaha.craigslist.org"), "quadcities" => array("http://quadcities.craigslist.org"), "siouxcity" => array("http://siouxcity.craigslist.org"), "ottumwa" => array("http://ottumwa.craigslist.org"), "waterloo" => array("http://waterloo.craigslist.org"), "KANSAS" => array("http://kansascity.craigslist.org","http://lawrence.craigslist.org","http://ksu.craigslist.org", "http://nwks.craigslist.org","http://salina.craigslist.org","http://seks.craigslist.org", "http://swks.craigslist.org", "http://topeka.craigslist.org","http://wichita.craigslist.org"), "kansascity" => array("http://kansascity.craigslist.org"), "lawrence" => array("http://lawrence.craigslist.org"), "ksu" => array("http://ksu.craigslist.org"), "nwks" => array("http://nwks.craigslist.org"), "salina" => array("http://salina.craigslist.org"), "seks" => array("http://seks.craigslist.org"), "swks" => array("http://swks.craigslist.org"), "topeka" => array("http://topeka.craigslist.org"), "wichita" => array("http://wichita.craigslist.org"), "KENTUCKY" => array("http://bgky.craigslist.org","http://cincinnati.craigslist.org","http://wichita.craigslist.org", "http://eastky.craigslist.org","http://huntington.craigslist.org","http://lexington.craigslist.org", "http://louisville.craigslist.org","http://owensboro.craigslist.org","http://westky.craigslist.org" ), "bgky" => array("http://bgky.craigslist.org"), "cincinnati" => array("http://cincinnati.craigslist.org"), "eastky" => array("http://eastky.craigslist.org"), "huntington" => array("http://huntington.craigslist.org"), "lexington" => array("http://lexington.craigslist.org"), "louisville" => array("http://louisville.craigslist.org"), "owensboro" => array("http://owensboro.craigslist.org"), "westky" => array("http://westky.craigslist.org") ); ?>
  2. Well, the problem is that (as I supposed in my first post): Your regular expression is build to expect that the FONT tag immediately follows the closing A tag. However, the pages you are scraping currently have have " - " between the anchor tag and the font tag, thus the preg_match returns no results.
  3. Um, no, you are scraping their content. This is YOUR code: $html = file_get_contents("$url/muc/"); All of the $url's you defined are at craigslist. You are obviously scraping their site. The fact that you are only scraping their links to point back to their site is beside the point. They want people to browse their site for these links - not yours. There does happen to be a copyright notice at the bottom of each of those pages you are scraping. And, is it really necessary to result to name calling. I mean really, I can't help it that you suck at programming.
  4. Seriously? You expect us to wade through all of that? There was no reason to post hundreds of lines of elseif's. That's some pretty inefficient code. Put your URLs into an array with the state as the key. No need to use endless elseif's. [quoe]... how can i find out of my code is not grabbing anything? Testing it would be the first thing that comes to mind. You need to test each step removing all other variables. So first, test that huge elseif section. If you pass the state ($st) is the proper array set? If no, fix it. If yes, then test the next bit of code. Looks like most of the functionality is in the last 20 lines or so. It appears you are doing some screen scraping and then modifying the content to get the data you need. Perhaps something has changed in the output that makes the "lean up" remove all the content. Did you verify if you received anything back? Then again, maybe cCraig's List decided it was tired of you steraling thier content and blocked your IP. Would serve you right.
  5. What problems are you experiencing? The code above has problems, but nothing that would obviously make the code not function. Here are some things to address: 1. The values are not escaped before running in a query. 2. You are doing a query in a loop. You shoudl do just one query with all the valules 3. The code to insert the data should be inside the IF condition. On second look, the problem may be that you are defining $facilities twice - once before the IF condition and once inside. If one fo the checkboxes are checked, then $facilities will be set as an empty value and the foreach will die. Plus, if the field "Add Details" is not passed inthe form, the code to insert the facilities values will not run. if (isset($_POST['Add Details'])) { if (isset($_POST['facilities']) && is_array($_POST['facilities'])) { $values = array(); foreach ($facilities as $facility_value) { //Add escaped values to an array $values[] = "('" . mysql_real_escape_string($facility_value) . "')"; } //Add ALL values to one query $query = "INSERT INTO production (facilities) VALUES " . implode(', ', $values); $result = mysql_query($query); } //Assume more code goes inside this IF condition }
  6. You will need to calculate the days between the date_created and today and then divide the hits by that number. BUT - you have to be careful of not doing a division by zero. I'm too lazy to try all o this out, but here is a quick run down of what I was able to google for in 30 seconds. Although, I have no idea why you would store the URL creation record and the hits in two separate tables. This is what I would try: SELECT links_table.URL, hits_table.hits as hits, DATEDIFF(CURDATE(), links_table.date_created) AS days, IF(days<1, hits/days, 0) as hits_per_day FROM links_table JOIN hits_table ON links_table.URL = hits_table.URL Although, I'm not 100% sure you can use a calculated value (i.e. days) in a subsequent equation. If not, then you would have to write out the entire formula: SELECT links_table.URL, hits_table.hits as hits, IF(DATEDIFF(CURDATE(), links_table.date_created)<1, hits/DATEDIFF(CURDATE(), links_table.date_created), 0) as hits_per_day FROM links_table JOIN hits_table ON links_table.URL = hits_table.URL Again, none of this is tested.
  7. Oh, I accidentally removed the actual WHERE text that I was defining. It would make sense to move that inside the first IF statement instead of doing another comparison: $where = ''; if (isset($keyword_search) || isset($category_search)){ $where_clause = array(); if (isset($keyword_search) && !empty($keyword_search)) { $where_clause[] = "(title LIKE '%{$keyword_search}%' OR info LIKE '%{$keyword_search}%')"; } if (isset($category_search) && !empty($category_search)) { $where_clause[] = "category LIKE '%{$category_search%}'"; } $where = "WHERE " . implode(' AND ', $where_clause); } $qry = "SELECT * FROM table {$where}"; $result = mysql_query($qry) or die("Doh!");
  8. Looks like $category_search is set as an empty string. I wrote my code using the same logic you had. You should add a secondary check to ensure the value is not an empty string. Personally, I prefer to echo the query to the page when it fails (in the development phase) so I can ensure it is as I expect. $where = ''; if (isset($keyword_search) || isset($category_search)){ $where_clause = array(); if (isset($keyword_search) && !empty($keyword_search)) { $where_clause[] = "(title LIKE '%{$keyword_search}%' OR info LIKE '%{$keyword_search}%')"; } if (isset($category_search) && !empty($category_search)) { $where_clause[] = "category LIKE '%{$category_search%}'"; } $where = implode(' AND ', $where_clause); } $qry = "SELECT * FROM table {$where}"; $result = mysql_query($qry) or die("Doh!");
  9. $where = ''; if (isset($keyword_search) || isset($category_search)) { $where_clause = array(); if (isset($keyword_search)) { $where_clause[] = "(title LIKE '%$keyword_search%' OR info LIKE '%$keyword_search%')"; } if (isset($category_search)) { $where_clause[] = "category LIKE '%$category_search%'"; } $where = implode(' AND ', $where_clause); } $qry = "SELECT * FROM table {$where}"; $result = mysql_query($qry) or die("Doh!"); NOTE: contained the keyword search in parens. Otherwise teh ANDs and ORs may not always react as you would want.
  10. jl5501 got the solution, but why would you use WHILE, LIST and EACH when a simple FOREACH loop will do??? You should also try to use descriptive variable names. $key and $value tells yo nothing of what the values actually are except that they probably came from an array. May not be an issue when writing the code, but it will save tons of time when you have to make changes later on. I'd also suggest separating the LOGIC from the OUTPUT. Put the PHP code to generate the content at the top of the page and output the content at the bottom: <?php $prices = array('Tyres' => 125.00, 'Oil' => 5.50, 'Sparkplugs' => 0.50); $priceList = "<ul>\n"; foreach($prices as $desc => $price) { $priceList .= "<li>{$desc} - {$price}<li>\n"; } $priceList .= "</ul>\n"; ?> <html> <body> Price list:<br /> <?php echo $priceList; ?> </body> </html>
  11. Well, this really isn't a PHP or even MySQL question. Moving topic. But, on the face of it, one solution would be to sort by the hits per day: TOTAL_HITS/DAYS_OLD. So, if you have a link that is one day old and it has 1 hit, it will be sorted above a link with 300 hits that was a year old. Trying to develope the "right" method of displaying the most relevant results is more of an art form than a skill. Personally, I would do something like hits per day AND also reserve some room at the top for 3-5 random NEW links - last day or two. Otherwise, new links will still not make the list as they will always start with 0.
  12. Here is a fully working test page implementing the code I included above. I also implemented a feature to reselect the previously selected value. Give it a try. <?php //Create the list of values $colorList = array('Red', 'Green', 'Blue', 'Yellow'); //Validate the input if(isset($_POST['color'])) { if (!in_array($_POST['color'], $colorList)) { $response = "You have selected an invalid option."; } else { $response = "You selected the color {$_POST['color']}"; } } else { $response = "No values have been posted yet."; } //Create the options $options = "<option value=\"\">--Select One--</option>\n"; foreach ($colorList as $color) { $selected = ($_POST['color']==$color) ? ' selected="selected"' : ''; $options .= "<option value=\"{$color}\"{$selected}>{$color}</option>\n"; } //Add an invalid option simulating a malicius user $options .= "<option value=\"Car\">Car</option>\n"; ?> <html> <body> <?php echo $response; ?> <br /><br /> <form method="POST"> Select a color: <select name="color"> <?php echo $options; ?> </select> <br /> <button type="submit">Submit</button> </form> </body> </html>
  13. You are missing a closing bracket for the opening OPTION tag when creating the options. The rendered HTML is being interpreted as empty options. $regOptions .= "<option value=\"{$att1fp}\">{$att1fp}</option>\n"; // ^--Add closing bracket here
  14. There's nothing specific I am aware of that passing data on the query string will remove line breaks. You didn't post any code so it is difficult, if not impossible, to troubleshoot your problem. Sounds like the data is passing through multiple technologies/processes. You will need to test the input/output at each stage to determine where the break is occuring. I did a simple test with sending data with line breaks on the query string and was able to use nl2br() without a problem.
  15. Why? You already have the data in a format that should be usable, why make it more complicated? Even though I think you are probably going about it wrong, I'll still provide a solution to what you asked. There is probably a more efficient solution, but htis works: function convertArray($inputArray) { end($inputArray); $lastArray = array(); $outputArray = array(); $outputArray[key($inputArray)] = current($inputArray); do { $lastArray = $outputArray; $outputArray = array(); $outputArray[key($inputArray)] = current($inputArray); $outputArray[key($inputArray)][key($lastArray)] = current($lastArray); } while (prev($inputArray)); return $outputArray; } $test = array( 1 => array( 'id' => 1, 'name' => 'Root', 'parent' => 0 ), 2 => array( 'id' => 2, 'name' => 'Child', 'parent' => 1 ), 3 => array( 'id' => 3, 'name' => 'Sub-Child', 'parent' => 2 ) ); echo "<pre>"; print_r($test); echo "\n\n"; print_r(convertArray($test)); echo "<pre>"; Output: Original Array: Array ( [1] => Array ( [id] => 1 [name] => Root [parent] => 0 ) [2] => Array ( [id] => 2 [name] => Child [parent] => 1 ) [3] => Array ( [id] => 3 [name] => Sub-Child [parent] => 2 ) ) Converted Array: Array ( [1] => Array ( [id] => 1 [name] => Root [parent] => 0 [2] => Array ( [id] => 2 [name] => Child [parent] => 1 [3] => Array ( [id] => 3 [name] => Sub-Child [parent] => 2 [3] => Array ( [id] => 3 [name] => Sub-Child [parent] => 2 ) ) ) ) )
  16. Actually, I don't think that is possible in principle either. When you use sessions, the session ID must be maintained by the client. This is normally done by the system creating a cookie on the user's system with the session ID but can also be done by propogating the session ID in the URL. This is all handled by the server. And, that cookie has an expiration such that it expires as soon as the browser is closed. So, the session is not maintained. But, I digress... So, you want a way to automatically authenticate the user when they return to the site. The only way to do that is with a cookie. So, the question really is what do you put in the cookie and maintain security. As oing as the password hash is properly salted any risk is probably mitigated. But, I think a better approach would be to utilize the session even after the session expires. Here is the process you could follow: Create a secondary table to maintain "offline" sessions. There would need to be two fields: userID and sessionID. After a user is authenticated create a record in that table with their userID and the current session ID AND save a cookie that does not expire with that session ID (this will be different from the session cookie). Let's call this the "Saved Session" cookie. So, now, when the user attempts to access the site and you determine they are not already authenticated check to see if they have the cookie for the saved session. If not, they go to the normal log in page. But, if they do, then authenticate them according to the user ID associatied with that saved session. AND update that record and the cookie with the new session ID. You would want to allow multiple saved sessions for a user in case they have multiple computers. So, one problem you would encounter is is a user deletes all their cookies. You will have orphaned data in that table. That may or may not be a problem depending on how often users delete their cookies and how many users and how much activity you have. Plus, a cookie could be copied to another computer. There is also another twist that could aleviate the orphaned records and address the security issue. In additin to saving the userID and the session ID you could save a value that is specific to the computer being used (in the database, not the cookie). Using that value you could delete orphaned records and ensure the cookie can't be used on another PC. I don't know what client specific values you can access via PHP if any. But, if you can't get them via PHP you could get them through a JavaScript workaround. Won't work in every instance, but then you just don't store a machine specific value in which case you are back to the original solution and it will still work.
  17. Well, that code is not properly formatted. I'm surprised the page is not generating errors. The first line of the IF statement has one opening paren, but two closing parens. It needs to look like this: if (($att1fp) == 'NULL') However, that is not how you should validate a select field. Never, ever trust input from a user. Just because you have a select list does not mean the user cannot submit values which you didn't intend to be in that list. Malicious users can replicate your form and manipulate it to submit whatever values they want. The good news is that validating a select list is pretty simple. The approach may change slightly based upon how you generate your list, but the concept is the same. If my list is a fixed list of values that is not stored in a database, I will typically use an array variable to store the values. If you are using the same PHP script to generate the form and process the form I will define the array on that page. If different pages need the list I will define the array on a separate page which is included by the pages that need it. I will then use the list to both generate the select list AND to validate the user input. Example: $colorList = array ('red', 'green', 'blue', 'yellow'); When creating the select list $colorOptions = "<option value=\"\">--Select One--</option>\n"; foreach ($colorList as $color) { $colorOptions .= "<option value=\"{$color}\">{$color}</option>\n"; } echo "<select name=\"color\">"; echo $colorOptions; echo "</select>"; Validating the input for the select list //Validate that the posted value is in the valid list of values if (!in_array($_POST['color'], $colorList)) { echo "You must select a color option"; } If your values are in a database it is just as easy. Just query the list when creating the select list. When validating, do a query to see if the value exists in the list (after properly escaping the value first).
  18. Well, truth be told, the phrase 'stay logged in forever' is just as poor of a phrase and is a complete misnomer. There is no way to stay logged in forever - there always is a session length. The process would more aptly be called "log me in automatically and silently". The process works as you expected. Data is stored in a cookie. When a page loads if it finds that the user is not logged in it will then check for information in a cookie to log the user in. This is all done seamelessly to the user.
  19. By "It gets logged out quite often at the moment" are you meaning that the user's session is expiring? I.e. they are inactive for a period of time? Assuming your session information is being consistently handled across the application, as long as the user is active (within the session time out period - say 20 minutes), the user would only lose their session if they are inactive for the session time-out period. If that is not the problem, then your session handling is defective somewhere, maybe not loaded on a certain page. If that is the problem, then increase the session time-out period in the server settings.
  20. But that excludes some valid possibilities, e.g. 001001001. however, that may or may not be an issue for the OP.
  21. This should be a little more efficient: $randNum = sprintf("%09s", rand(0, 999999999));
  22. Oh really!? What is that first tag after the FORM tag? <form method="GET"> <textarea name="text"><?php echo $text; ?></textarea> <input type="text" name="idx" value="<?php echo $idx; ?>"> <button type="submit" name="submit">Submit</button> </form> You didn't provide ALL of the relevant code for your project - so I winged it as best I could. The code I provided does work - at least how I interpreted your requirements. Did you even try the code I posted? It is a completely self-contained script that you can run. If it seems to work how you want, you can then repupose it with slight modifications to your exisitng code.
  23. You will never be able to determine the "new" input using regular expression. You can probably come close, but I would juse use an index value on each submission. The following code is modified from your to just use a form instead of writing to a file as I don't know how you were using it for display. But, this shoudl get you in the right direction. <?php function stripslashes_deep($value) { $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); return $value; } if (get_magic_quotes_gpc()) { $_GET = stripslashes_deep($_GET); } if (!isset($_GET['idx'])) { $idx = 115; $text = "Microsoft Windows [Version 6.1.7600]\n"; $text .= "Copyright (c) 2009 Microsoft Corporation. All rights reserved.\n"; } else { $idx = $_GET['idx'] + 3; $input = $_GET['text']; $text = substr($input, 0, $idx); $user_input = substr($input, $idx); $text .= "\n'{$user_input}' is not recognized as an internal or external command,\noperable program or batch file.\n"; } $text .= "\n"; $text .= "C:WINDOWS>"; $idx = strlen($text); //echo strlen($text); //$fn = "text.txt"; //$file = fopen($fn, "a"); //fwrite($file, "$msg2"); //fclose($file); ?> <html> <style> body{ background-color:#000000;color:#ffffff } textarea { background-color:#000000; color:#ffffff; width:100%; height:400px; } </style> <body> <form method="GET"> <textarea name="text"><?php echo $text; ?></textarea> <input type="text" name="idx" value="<?php echo $idx; ?>"> <button type="submit" name="submit">Submit</button> </form> </body> </html>
  24. You need to create the OPTIONS inside the loop. Then after you have generated all the options, then create the SELECT field and add the options. <?php //Query the data $username = mysql_real_escape_string($_SESSION['userName']); $get_scouts = "select * from scouts where username = '{$username}'"; $get_scouts_res = mysql_query($get_scouts, $conn) or die(mysql_error()); //Create the options $scout_options = ''; while ($list_scouts = mysql_fetch_array($get_scouts_res)) { $identity = ucwords($list_scouts['identity']); $topic_id = $list_scouts['id']; $scout_options .= "<option value=\"{$topic_id}\">{$identity}</option>\n"; } //Create the select list echo "<select name=\"scount_list\">\n"; echo $scout_options; echo "</select>\n"; ?>
  25. Did you read ALL of my post? I covered that in the last paragraph. While it would be advantageous to create it such that it *could* be modified int he future to handle multiple associations, the fact is that was not stated as a requirement and this solution would be easier to implement. I see way too may people that don't know how to JOIN tables properly - if at all. So, the less JOINs needed the better. BUt, if you want such a solution, fine... Create one table for all the categories. Each category record will have a column to identify the parent category - if one exists. Then the product table would have no direct associattions at all. Instea you would have an intermidiary table to make the associations between products and categories. You could then associate a product to any number of categories and sub categories. So, if product X is associated with subCategory C (level 3), you would also associate it with the parent subCategory B (level 2) and that category's parent, category A (level 1/parent). Then by JOINing the three tabels you could get all the products associated with a category at any level.
×
×
  • 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.