dragonfx76 Posted December 12, 2016 Share Posted December 12, 2016 (edited) Hello there Thanks to some help on this forum i have a working php file. I'm tweaking it at the moment so it will give me a certain text when there is no data found in a database.. The problem is, i think i got the logic, just don't know the code for it. Let me start with showing the code: <?php $dbhost = 'localhost'; $dbuser = '**********'; $dbpass = '**********'; $database = '**********'; ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); if (isset($_GET['zoeknummer'])) { $pdo = new PDO("mysql:host=$dbhost; dbname=$database", $dbuser, $dbpass); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $sql = "SELECT lead_content FROM wp_wgbsupicrm_leads"; $res = $pdo->query($sql); // where $pdo is your db connection while ($lead = $res->fetchColumn()) { $data = json_decode($lead); if ($data->zoeknummer == $_GET['zoeknummer']) { echo "Uw plaats voor " ; echo $data->wachtlijstkomplex . ' : ' . $data->wachtlijstplaats . '<br>'; } else { echo 'Geen resultaat gevonden met opgegeven nummer. <br> Kijk na of U het nummer correct heeft ingevuld.'; } } } ?> I understand that the 'while' makes it search the whole database and the if will show the result found. I also understand that the else will show the text when there is no data found in that column, and i understand it will do that for each coluumn no data is found, and there is the problem. I only want it to show the text displaying once. No mather howmuch columns it has searched in. This code now also shows text for all espty searches and will show the result in between all the negative results. The logic i get is that i need to get the 'else' out the 'while' but that's it. Any help would be appreciated, i'm still trying to understand this php language and am learning, I know the solution might be as simple as breaking an egg.. but if u don't know what an egg is.. ;-) Thanks in advance! Edited December 12, 2016 by dragonfx76 Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 12, 2016 Share Posted December 12, 2016 You could use a flag (true/false) which starts off as FALSE, but then can be set to TRUE in your IF LOOP. When you are done with the WHILE LOOP, check to see if the flag is set, and if so, print your text. Based on your desired presentation, you might need to not directly echo the column text, but either add them to a string or buffer them (http://php.net/manual/en/function.ob-flush.php) Quote Link to comment Share on other sites More sharing options...
dragonfx76 Posted December 12, 2016 Author Share Posted December 12, 2016 Hi there, thanks for your answer. I understand what you say, just don't know what it's supposed to be in code. Also will this not 'overwrite' previous found results? In the loop there can be multiple results where 'zoeknummer' may result in more then 1 result to 'echo'. If i get the if and echo out it will only show the last result of the query? if (isset($_GET['zoeknummer'])) { $pdo = new PDO("mysql:host=$dbhost; dbname=$database", $dbuser, $dbpass); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $sql = "SELECT lead_content FROM wp_wgbsupicrm_leads"; $res = $pdo->query($sql); // where $pdo is your db connection while ($lead = $res->fetchColumn()) { $data = json_decode($lead); if ($data->zoeknummer == $_GET['zoeknummer']) { echo "Uw plaats voor " ; echo $data->wachtlijstkomplex . ' : ' . $data->wachtlijstplaats . '<br>'; } else { echo 'Geen resultaat gevonden met opgegeven nummer. <br> Kijk na of U het nummer correct heeft ingevuld.'; } } } So your suggestion in to make a FALSE flag that would 'echo' what i have now at 'else'? and it would change to 'TRUE' if there is a result found in the loop? As i said, i have no idea how this would work in code, going to look this up, hope maybe in the meantime i can get some explained here aswell? Thanks for the reply! Working on it :-) Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 12, 2016 Share Posted December 12, 2016 I understand that the 'while' makes it search the whole database and the if will show the result found. I also understand that the else will show the text when there is no data found in that column, and i understand it will do that for each coluumn no data is found, and there is the problem. I only want it to show the text displaying once. No mather howmuch columns it has searched in. This code now also shows text for all espty searches and will show the result in between all the negative results. By the way, your statement "the else will show the text when there is no data found in that column" is incorrect. I will show when, $data->zoeknummer is not equal to $_GET['zoeknummer'] Also, not positive about what you are saying above so maybe something like the following won't work for you. $flag=false; while ($lead = $res->fetchColumn()) { $data = json_decode($lead); if ($data->zoeknummer == $_GET['zoeknummer']) { echo "Uw plaats voor " ; echo $data->wachtlijstkomplex . ' : ' . $data->wachtlijstplaats . '<br>'; } else { $flag=true; } } if($flag) { echo 'Geen resultaat gevonden met opgegeven nummer. <br> Kijk na of U het nummer correct heeft ingevuld.'; } Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted December 12, 2016 Share Posted December 12, 2016 if you are not tied to this database design, you should change it to properly store the data. by storing json encoded data, you cannot directly find matching data using the sql query. this will also not scale well as you must retrieve all the data in order to scan it for matches. this will result in a poorly performing web site when there is a moderate to large amount of data. if the data is stored properly, by storing the 'columns' in the json data in individual database table columns, you can find the row(s) that match the search term, directly in the sql query statement and all the code will be simplified. for your current design, the easiest way of using the FLAG method would be to use an array and store any data that matches the search term into the array. after you finish looping over the data, if the array is empty, there were no matches. if the array is not empty, loop over the entries in it and produce the output from the data. Quote Link to comment Share on other sites More sharing options...
dragonfx76 Posted December 12, 2016 Author Share Posted December 12, 2016 if you are not tied to this database design, you should change it to properly store the data. by storing json encoded data, you cannot directly find matching data using the sql query. this will also not scale well as you must retrieve all the data in order to scan it for matches. this will result in a poorly performing web site when there is a moderate to large amount of data. I understand this way of working isn't the best, the database i'm using is created by a plugin i'm using on a wordpress site. i'm afraid i'm stuck with that. What you say about the Array sounds logical, and was indeed something i thought of, again, the logic is there, the code not. It would mean that where now if ($data->zoeknummer == $_GET['zoeknummer']) { echo "Uw plaats voor " ; echo $data->wachtlijstkomplex . ' : ' . $data->wachtlijstplaats . '<br>'; } is the echo should be replaced with something to make the data found in a so called array? then further down the code else { echo 'Geen resultaat gevonden met opgegeven nummer. <br> Kijk na of U het nummer correct heeft ingevuld.'; } this should be gone i geuss.. I hope atleast i get the logic.. next thing i would think of then is that there should be a line saying that 'if' 'array' has content then if (array has data) { echo "Uw plaats voor " ; echo $data->wachtlijstkomplex . ' : ' . $data->wachtlijstplaats . '<br>'; } else { echo 'Geen resultaat gevonden met opgegeven nummer. <br> Kijk na of U het nummer correct heeft ingevuld.'; } So the problem i have now is how do i get that in code? atleast... If my logic does make any sense... Quote Link to comment Share on other sites More sharing options...
dragonfx76 Posted December 12, 2016 Author Share Posted December 12, 2016 Hey NotionCommotion, I tried out your solution, it ends up the same way it does without it, it gives an 'echo' for each line it looks in. Thanks agian for helping me out here, i'm learning things even if it isn't the solution so i'm very thankfull. Quote Link to comment Share on other sites More sharing options...
Solution dragonfx76 Posted December 12, 2016 Author Solution Share Posted December 12, 2016 Hey all, thanks for the help, i found a working solution: if (isset($_GET['zoeknummer'])) { $pdo = new PDO("mysql:host=$dbhost; dbname=$database", $dbuser, $dbpass); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $sql = "SELECT lead_content FROM wp_wgbsupicrm_leads"; $res = $pdo->query($sql); // where $pdo is your db connection $flag=false; while ($lead = $res->fetchColumn()) { $data = json_decode($lead); if ($data->zoeknummer == $_GET['zoeknummer']) { echo "Uw plaats voor " ; echo $data->wachtlijstkomplex . ' : ' . $data->wachtlijstplaats . '<br>'; $flag=true; } } if ($flag==false) { echo 'Geen resultaat gevonden met opgegeven nummer. <br> Kijk na of U het nummer correct heeft ingevuld.'; } } Posting it for further reference thanks all again, you guy's helped alot getting to this! Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted December 12, 2016 Share Posted December 12, 2016 example of the array/flag method, that has the added advantage of separating the database specific logic from the presentation logic, so that if you do end up with a correctly designed database table, you only have to change the database specific logic. the presentation logic would remain the same. if (isset($_GET['zoeknummer'])) { $pdo = new PDO("mysql:host=$dbhost; dbname=$database", $dbuser, $dbpass); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $sql = "SELECT lead_content FROM wp_wgbsupicrm_leads"; $res = $pdo->query($sql); // where $pdo is your db connection $matches = array(); // array/flag to hold the matching data while($lead = $res->fetchColumn()) { $data = json_decode($lead); if ($data->zoeknummer == $_GET['zoeknummer']) { $matches[] = $data; } } // presentation logic - produce the output from the data if($matches) { foreach($matches as $data) { echo "Uw plaats voor " ; echo $data->wachtlijstkomplex . ' : ' . $data->wachtlijstplaats . '<br>'; } } else { echo 'Geen resultaat gevonden met opgegeven nummer. <br> Kijk na of U het nummer correct heeft ingevuld.'; } } Quote Link to comment Share on other sites More sharing options...
benanamen Posted December 12, 2016 Share Posted December 12, 2016 (edited) You don't need all that flag stuff and a "working solution" doesn't make it right. Of course you will want to use htmlentities on the output. It also looks like you should be using a WHERE clause since you are only looking for a particular result that matches $_GET. <?php $sql = "SELECT column_name FROM table"; $stmt = $pdo->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll(); if ($result) { foreach ($result as $row) { echo "{$row['column_name']}<br>"; } } else { echo 'No Results'; } ?> Edited December 12, 2016 by benanamen Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted December 12, 2016 Share Posted December 12, 2016 (edited) i found a working solution: Calling this is a “solution” is somewhat overly optimistic. As long as your database layout is fundamentally broken, you'll run into problems over and over and over again. This was a friggin' data lookup, one of the most trivial operations possible. Yet you needed two communities, dozens of lines of code and probably many hours or even days to get it done. Now imagine having to solve a real problem. I don't know where you got your “plugin” from, but I suggest you either fix it or throw it away altogether. It also helps a lot to use proper English names, because that's the de-facto standard in programming and makes sure everybody can understand you. I'm not going to run each line of your code through Google Translate, especially when the translation results are utter nonsense (I can't tell if that's your fault or Google's). With a correctly designed database, the entire lookup is just one trivial query: $stmt = $databaseConnection->query(' SELECT somecomplex, someplace FROM idontknow WHERE somenumber = ? '); $stmt->execute([$_GET['somenumber']]); $result = $stmt->fetch(); // if you expect more than one match, use fetchAll() if ($result) { echo 'Somecomplex '.html_escape($result['somecomplex']).', someplace '.html_escape($result['someplace']); } else { echo 'Invalid number.'; } No loops, no flags, no filtering. Just plain old SQL. Edited December 12, 2016 by Jacques1 Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 12, 2016 Share Posted December 12, 2016 You don't need all that flag stuff... I understood the requirements to be display the text Geen resultaat gevonden met opgegeven nummer. <br> Kijk na of U het nummer correct heeft ingevuld. zero or one time only. But then again, I stated I really did not understand the requirements. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.