Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
Was Working, But now it is returning a blank display?
Psycho replied to Modernvox's topic in PHP Coding Help
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. -
Was Working, But now it is returning a blank display?
Psycho replied to Modernvox's topic in PHP Coding Help
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. -
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 }
-
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.
-
How do you build a query from a number of search fields
Psycho replied to 86Stang's topic in PHP Coding Help
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!"); -
How do you build a query from a number of search fields
Psycho replied to 86Stang's topic in PHP Coding Help
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!"); -
How do you build a query from a number of search fields
Psycho replied to 86Stang's topic in PHP Coding Help
$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. -
Loop is dislayed with empty li's between filled li's
Psycho replied to ab2qik's topic in PHP Coding Help
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> -
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.
-
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>
-
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
-
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.
-
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 ) ) ) ) )
-
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.
-
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).
-
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.
-
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.
-
But that excludes some valid possibilities, e.g. 001001001. however, that may or may not be an issue for the OP.
-
This should be a little more efficient: $randNum = sprintf("%09s", rand(0, 999999999));
-
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.
-
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>
-
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"; ?>
-
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.
-
I would simply associate each product with all four levels of groupings ass applicable. So, let's assume you have four tables for each level: Section 1 (e.g. Furniture) Section 2 (e.g. Living Room) Section 3 (e.g. Sofas) Section 4 (e.g. Sleepers) Every lower section will have a parent ID field that relates it to the next higher level (i.e. Sofas will be associated with Living Room). So, you don't have to have child records for each possible parent record. Then, for your products, have four separate fields to identify the relationship between the product and all four levels of sections. If you have a product that is related to records at sections 1 & 2, then the fields for sections 3 & 4 will remain NULL. So, if you need to find all products related to Furniture, just query all products where the ID in the section1 field is the same as the ID for furniture. Now, if you need to relate products to multiple values int eh same section it gets more complicated, But, you didn't state thar requirement.
-
Then you just need to have a table with the questions and build a form that displays the question, has a hidden input for the question ID and a text box for the response. Veery straight forward.