Jump to content

NiTx

Members
  • Posts

    31
  • Joined

  • Last visited

Everything posted by NiTx

  1. You can't echo operations, You need to use if or else statements. $operation = $_POST['SelectedOperation'] if($operation == 'count') { echo count($array); } elseif ($operation == 'sum') { echo array_sum($array); } You can have as many elseif statements as you require.
  2. Solved the issue. Discovered LEFT JOIN. As with most of my php problems, I just don't know what to search in Google to find the answers to my issue.. For anyone else with the same issue this is how I solved my issue. $sql = "(SELECT devices.id, devices.brand_id, devices.dev_model, devices.dev_version, devices.dev_model_number, devices.dev_release_month, devices.dev_release_year, brands.brand_name FROM devices "; $sql .= "LEFT JOIN brands ON devices.brand_id = brands.id "; $sql .= "WHERE CONCAT(brands.brand_name ,' ', devices.dev_model, ' ', devices.dev_version, ' ', devices.dev_model_number) LIKE '%{$database->escape_values($term)}%' "; $sql .= "ORDER BY devices.dev_release_year DESC, devices.dev_release_month DESC LIMIT 4)";
  3. NiTx

    help

    Sorry to say this but you clearly have no idea what you're doing. You're missing " " around your query. That to any basic php programmer would be obvious. I'd suggest you take the time to learn PHP before asking for help.
  4. It looks like you've been watching Lynda tutorials? Make sure you don't skip any of the videos and try actually MAKE SENSE of what your learning. The error your getting spells it out for you, the fetch_assoc method doesnt exist.. Plain and simple, you need to add it. I will spoon feed you. Add this to your database class. (I've added extras for your convenience) public function fetch_assoc($result_set) { return mysql_fetch_assoc($result_set); } public function num_rows($result_set) { return mysql_num_rows($result_set); } public function fetch_array($result_set) { return mysql_fetch_array($result_set); }
  5. I have two tables, brands and devices. brands table is set up like so: id | brand_name 1 Apple 2 Samsung devices: id | brand_id | dev_model | dev_version 1 1 iPhone 5s 2 2 Galaxy S4 Right now I can search for a phone by its device name, for example: 'iPhone 5s', but I need my users to be able to search for the brand too: 'Apple iPhone 5s'. Is there a practical approach to doing this without having to add the brand names in my device table? This is my current MySQL query: $sql = "(SELECT id, brand_id, dev_model, dev_version FROM devices WHERE "; $sql .= "CONCAT(dev_model, ' ', dev_version) LIKE '%{$database->escape_values($term)}%' "; $sql .= "LIMIT 4)"; Any help would be appreciated, or even a point in the right direction.
  6. Hi, I'm trying to build a search function for my website with a jQuery auto-complete feature implemented. I have three columns I need to search for in my database, dev_model, dev_version and dev_model_number. Right now I'm just focusing on getting the query to work with the first two columns. The auto-complete works, but its far from perfect and gives undesired results at times. I've tried multiple different approaches to building my search query. So far, doing a full text search is working the best but its far from perfect. Here's my MYSQL query: SELECT id, brand_id, dev_model, dev_version, MATCH (dev_model,dev_version) AGAINST ('+" . $database->escape_values($parts[$i]) . "*' IN BOOLEAN MODE) AS SCORE FROM devices WHERE MATCH (dev_model,dev_version) AGAINST ('+" . $database->escape_values($parts[$i]) . "*' IN BOOLEAN MODE) ORDER BY SCORE DESC LIMIT 4 My search page: http://www.fonefox.com/fonefox/public/index1.php If you search for example 'Galaxy S4', as soon as you start typing the 'S4' (dev_version), the autocomplete hides the Galaxy S4 phone from the search. No idea why, but if the 'dev_version' string is greater then 4 characters it WILL work. Another issue is, you could search 'Galaxy S4 Lumia 1020', and it will return results for a Lumia 1020 which has no relevance to "Galaxy" or "S4". I'm guessing that I need to somehow concatenate dev_model and dev_version before comparing my search terms to it. I'm just not sure how to approach this. Any help would be greatly appreciated.
  7. Thanks trq, I see what you mean. I'm trying to get myself familiar with OOP concepts but i'm failing to do so! :S Is pulling in from the global scope a complete no-no? Take my basic find_by_sql function as an example.. Do you not have a database class to make database queries? And if you do, how do you make queries without pulling it in form the global scope? public static function find_by_sql($sql="") { global $database; $result_set = $database->query($sql); $object_array = array(); while ($row = $database->fetch_array($result_set)) { $object_array[] = self::instantiate($row); } return $object_array; }
  8. All classes that are associated to a table in my database Inherit this class as it contains my Create, Update and Delete functions. So as to save me rewriting that code multiple times in each class. Sorry, I'm still very new to OOP so I wouldn't know why. Seems still viable to me, and to the person who's tutorial I've been watching. I'm all ears though, the tutorials I've been watching are aged and I don't want to start any bad habits early. Your're right. It is only for me but I plan to have admins that could make mistakes. It's really hard to explain what I'm asking you guys without posting a ton of code. I should have spent more time structuring my post as to explain myself better. To put it into perspective.. Lets say you had a database table with 110 columns and a form with 100 fields. You have 60 fields which are required fields, 40 fields that are not required. The remaining 10 variables are generated fields like id, admin_id and date_added etc.. All these different variables have to go through a different process in my code before getting submitted to the database. So have I done the right thing by grouping my variables into arrays so that each group of variables can be processed in the appropriate way?
  9. Hey Guys, I've just started to learn OOP. So far I'm lovin' it, but I want to get some advice. I'm dealing with a form that has around 100 fields and I'm trying to find the most efficient way to work my variables. Here's a small example. class Review extends DatabaseObject { protected static $table_name = "reviews"; public static $required_fields = array( 'security', 'location', 'training', 'comments' ); public static $nonRequired_fields = array( 'pay', 'pay_rate', 'tips' ); public static $generated_fields = array( 'company_id', 'id', 'user_id', 'date_added' ); protected function attributes() { global $company; global $user; $attributes = array(); foreach(self::$required_fields as $field) { if(property_exists($this, $field)) { $attributes[$field] = $this->$field; } } foreach(self::$nonRequired_fields as $field) { if(property_exists($this, $field)) { $attributes[$field] = $this->$field; } } if(isset($_SESSION['user_id'])) { $attributes['user_id'] = $_SESSION['user_id']; } $attributes['company_id'] = $company->company_id; $attributes['total_score'] = $this->total_score; return $attributes; } As you can see I've put these variables into arrays to make form validation easier, but when it comes to instantiating my class, I run into some issues. I know this is most likely a stupid approach. I apologize because I know I haven't really explained what I'm after. I have to be as efficient as possible since I'm dealing with such a large number of form fields and I'm not sure which way is the best approach with OOP. I hope my code is enough for you to get a good enough idea. Please help and thanks
  10. $q = "SELECT * FROM questions WHERE id = '$post'"; should be $q = "SELECT * FROM questions WHERE id = {$post}";
  11. Never used 'continue' before so thanks! Added to my repertoire Again, thank you both!
  12. Thanks requinix, I thought of this, but if my function passes the key ID andits value, I get an error. I could try to alter my function to suit, but it would be a pain in the ass to say the least. Still, thanks for the tip
  13. Hey guys, I'm trying to run a while loop for an array that includes an ID value in each row. I'm trying to temporarily disable that value while I perform a foreach loop, then prepend the value back into the array after the foreach loop is finished. function array_unshift_assoc(&$arr, $key, $val) { $arr = array_reverse($arr, true); $arr[$key] = $val; $arr = array_reverse($arr, true); } So far I've come up with this function (which works), but I feel like there may be an easier way? Rest of my code is below. Thoughts? while($row = mysql_fetch_assoc($get_specs)) { //Set the phones id to a temporary variable. $tempid = $row['id']; //Unset the id so that we can perform the foreach loop without //it interfering with the function. unset($row['id']); foreach($row as $spec => $value) { get_spec_rating($spec, $value); echo $rating . "<br/><br/>"; } //Unshift the id value back into the array array_unshift_assoc($row, "id", $tempid); } Thanks!
  14. Just goes to show you can only learn so much on lynda.com. My coding skills are still at a rudimentary level, so thanks! Will definitely read up some more on the implode. Given all the large arrays I use, I think it will come in handy! Nice to know I was on the right track! Thanks for introducing me to this, I've seen it used before but didn't know what to search in google. I did some reading on it just now, would you say it's mainly used for smaller if else statements like mine and that for a larger argument you would just use an ELSE IF? Again thanks a ton!
  15. Sorry, it was originally '{$_POST[$fieldname]}' I just rewrote the code in a hurry. As you pointed out, what happened was it created a new row for every value. I'm not sure how to make a loop where it doesn't create a new fow for each value (still researching, but if you can show me that would be fantastic). However, like I said before I have a solution which is to do this if(isset($_POST['freq_edge'])) { $freq_edge = mysql_prep($_POST['freq_edge']); } else { $freq_edge = 0; } I'm just not convinced that its kosher. I want to find a better solution, or have someone point me in the right direction to one.
  16. Hi there, I have a form with around 100 variables. Some variables are required to be filled in order for the form to be process, while some are not. At the moment if a non-required field isn't filled in, the form WILL submit, but I recieve a undefined variable error at the top of the page after submission. Now, I initially tried to submit the non required fields like this foreach($notRequired_fields as $fieldname) { if (isset($_POST[$fieldname])) { $query = "INSERT INTO smartphones ( $fieldname ) VALUES ( '{$fieldname}', )"; } This worked but failed misserable as you could imagine (should have seen it coming). So what I plan to do next is to make all the unset variables = 0 to get rid of the warning. Is this a good idea or bad practice? What's good practice when it comes to non required fields in a database? I ask because im still learning and I'm not sure if in the future It will be punitive for me to have empty rows in my database. If it IS ok to leave the variables empty in the database, how do I go about getting around the undefined variable error? Thanks in advance!
  17. You would need to add some form vaildation. Won't take you long to learn it. http://lmgtfy.com/?q=php+form+validation You can find video tutorials on form validation through youtube too. Good luck
  18. Ahhh silly me. Thank you kind sir - that did the trick :-)
  19. Hi, I'm trying to make a form that validates any errors on a page then submits the data via a $_SESSION to the next page for further review. I'm at the point where I'm trying to make it so that if there are any errors on the first page that form information isn't lost when the page reloads. So far this is fine for my text fields, drop down boxes etc, however I'm having issues with my check boxes. If they have been checked and submitted and the admin wants to uncheck it, when the page reloads it still comes back as being checked which means my code for unsetting the variable isnt working for whatever reason. Heres my code. <?php require_once("includes/session.php"); ?> <?php if(isset($_POST['submit'])) { $errors = array(); $required_fields = array( 'brand_id', 'dev_model', 'freq_2g_gsm850', ); //If any of the required fields are not set or empty, put them into an errors array and clear the $_SESSION variable foreach($required_fields as $fieldname) { if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) { $errors[] = $fieldname; unset($_SESSION['$fieldname']); } } //If there are no errors go ahead and process the data if (empty($errors)) { //Process each variable and add it to the Session for the next page to process *** Might be able to remove if(isset) foreach($required_fields as $fieldname) { if (isset($_POST[$fieldname])) { $_SESSION[$fieldname] = mysql_prep($_POST[$fieldname]); } } //Redirect to posttest.php to complete the device registration form redirect_to('posttest.php'); } else { foreach($required_fields as $fieldname) { if (isset($_POST[$fieldname])) { $_SESSION[$fieldname] = mysql_prep($_POST[$fieldname]); } } //unset($_SESSION['$freq_2g_gsm850']); //Errors occured $message = "There were " . count($errors) . " errors in the form"; } } ?> <h3>2G GSM</h3> <?php if(!isset($_SESSION['freq_2g_gsm850']) || empty($_SESSION['freq_2g_gsm850'])) { echo "<label><input type=\"checkbox\" name=\"freq_2g_gsm850\" value=\"850\" id=\"freq_2g_gsm850\">850</label>"; } else { echo "<label><input type=\"checkbox\" name=\"freq_2g_gsm850\" value=\"850\" id=\"freq_2g_gsm850\" checked>850</label>"; } ?> Thanks in advance for your help
  20. Achillies - please post your code properly by using the <> icon rather than just pasting it in the comments. No biggie, just make everyones life a lot easier. <?php $reason = "It's far more readable"; echo $reason; ?>
  21. Ahhhh such a simple solution. Thank you so much xyph. I'll keep this in mind for future coding :-)
  22. Sorry my mistake. It's meant to be if(!$device_set) { echo "error"; } This part of the code was a shot in the dark. I didn't think it would work because I should really be testing $devices, but like I said, I can't execute the code inside my while loop. There's probably a really simple solution (I've tried to google it) but I cant figure it out. Essentially I just want to echo an error if there are no devices added in my database for any particular brand.
  23. Hi, I'm trying to execute a while loop which by itself works fine. But I have no idea how to echo an error message if the while loop fails when there is nothing in the database. This is what I've tried so far: if($sel_brand) { $device_set = get_devices_by_brand($sel_brand['id']); if($device_set) { echo "error"; } else { while($devices = mysql_fetch_array($device_set)) { echo "<tr><td class=\"video_description\">" . "<a href=\"edit_device.php?device=" . urlencode($devices["id"]) . "\">" . "{$devices['dev_model']}" . " " . "{$devices['dev_version']}" . "</a></td></tr>"; } //end while loop }//end if($device_set) }//end if($sel_brand) I can see why my code isn't working, but I also know I cant execute my error code in the while loop. I'm really stuck here guys, if you could help that would be great :-)
  24. Thanks for your reply! Really helpful so thank you very much! It's good to know that as long as you make a site that's actually worth visiting (dynamic or static) it will not go unnoticed. Again, thanks! :-) Great advice, just did some research on this now and it's exactly what I need. I had no idea this was even an option so thanks a bunch! :-)
×
×
  • 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.