PHP - Suppress Duplicated Results
Hello,
I have 3 tables for clients, orders and the products they ordered.
Here are the tables...
Clients CLIENT_ID CLIENT_NAME Orders o_id client_id o_number o_date o_notes Products o_id qty name I am getting the results I need with this query... SELECT clients.CLIENT_ID, clients.CLIENT_NAME, orders.o_id, orders.client_id, orders.o_date, orders.o_number, orders.o_notes, products.o_id, products.qty, products.name FROM clients, orders, products WHERE clients.CLIENT_ID = orders.client_id AND orders.o_id = products.o_id AND orders.o_id = $theOrder..and this is the php I use to display it... while ($row = $result->fetch_assoc()) { echo "<h2>$row[CLIENT_NAME]</h2>"; echo '<label>Date</label><input type="text" value="'.timechange($row['o_date']).'" readonly><br>'; echo '<label>Order No.</label><input type="text" value="'.$row['o_number'].'" readonly><br>'; echo '<label>Notes</label><input type="text" value="'.$row['o_notes'].'" readonly>'; echo '<label>Product Qty.</label><input type="text" value="'.$row['qty'].'" readonly><br>'; echo '<label>Product Name</label><input type="text" value="'.$row['name'].'" readonly><br>'; }However - rather predictably I am getting three forms printed out as the results from MySQL look like this... mysql0.jpg 33.47KB 0 downloads Is it possible to somehow get my form to display the results, like this... mysql1.jpg 22.54KB 0 downloads I hope I'm explaining myself well enough - and thank you for reading. Edited by tHud, 04 July 2014 - 11:06 AM. Similar TutorialsI use PHP's PDO extensively for data grabbing and inserting. When I am selecting data, the return is a PHP PDO object which in itself contains select statements. So I use foreach to parse through the object and build a table. For cells which I have data it's fine but for cells which do not get any data meaning that have no entry in the db, php throws out a error PHP Warning: Invalid argument supplied for foreach(). I tried using if(is_array) and also @ to suppress them but no luck . Can anyone point me to the right direction for suppressing these errors/warnings!! Hey guys, So when I put the following line of php on an html page: Code: [Select] echo '®'; I get the 'Registered' symbol. How do I turn this off? What is happening is that it is part of a longer string that represents an url and the URL is not rendering correctly due to the special character. Thanks Hello, I'm trying to code a private message system and got stuck (yup, again) on this part: At the page where the messages are read, I'd like to display all the messages where the same sender and the receiver participate, like this: Code: [Select] SELECT * FROM messages WHERE sender = 1 AND receiver = 2 OR sender = 2 AND receiver = 1 ORDER BY date LIMIT 0,10; Is there anyway to do something like that? Thank you. (Again, sorry for the weird Subject, I gave my best) Hello. I have here a php code getting information from mysql but when printing the result is duplicated, or more. Code: [Select] <?php $query = "SELECT SUM(amount) FROM user_detail"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo "<p align=\"left\">Total ".$row['SUM(amount)']."</p>"; } ?> and then I get result like this: you can see there are 3 result and I need to print only one time. I use for sql connection other file with : include_once("./includes/config.php"); Need help to solve this Thanks in advance Hi guys, I am currently stuck in a situation. I have duplicated names in my form. View duplicated.jpg for a reference. What exactly went wrong? Any help will be greatly appreciate! How do I eliminate the duplicated names. Thanks!! And my $count is currently not working as well, it is supposed to be 5 columns per row. Code: [Select] <?php /***Pre-School Level***/ echo '<input name="level[]" type="checkbox" id="level_1" value="1">'; echo '<span class="zone_text_enlarge"><label for="level_1">Pre-School</label></span><br/>'; $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die(mysqli_error($dbc)); $query = "SELECT sl.subject_level_id, sl.level_id, sl.subject_id, tl.level_name AS level_name, ts.subject_name AS subject_name " . "FROM tutor_subject_level AS sl " . "INNER JOIN tutor_level AS tl USING (level_id) " . "INNER JOIN tutor_subject AS ts USING (subject_id) " . "ORDER BY subject_level_id ASC LIMIT 7"; $sql = mysqli_query($dbc, $query) or die(mysqli_error($dbc)); $query1 = "SELECT subject_level_id FROM tutor_overall_level_subject WHERE tutor_id = '" . $_GET['tutor_id'] . "'"; $sql1 = mysqli_query($dbc, $query1) or die(mysqli_error($dbc)); $selected_subjects = array(); while ($row = mysqli_fetch_array($sql1)) { array_push($selected_subjects, $row['subject_level_id']); } echo'<table><tr>'; // Start your table outside the loop... and your first row $count = 0; // Start your counter while($data = mysqli_fetch_array($sql)) { /* Check to see whether or not this is a *new* row If it is, then end the previous and start the next and restart the counter. */ if ($count % 5 == 0) { echo "</tr><tr>"; $count = 0; } foreach($selected_subjects as $selected_subject) { if ($data['subject_level_id'] == $selected_subject) { echo '<td><input name="subject_level[]" class="subject_a" type="checkbox" checked="checked" id="subject_level_'.$data['subject_level_id'].'" value="'.$data['subject_level_id'].'"/>'; echo '<label for="subject_level_'.$data['subject_level_id'].'" class="subject_1">'.$data['subject_name'].'</label></td>'; $count++; //Increment the count } else { echo '<td><input name="subject_level[]" class="subject_a" type="checkbox" id="subject_level_'.$data['subject_level_id'].'" value="'.$data['subject_level_id'].'"/>'; echo '<label for="subject_level_'.$data['subject_level_id'].'" class="subject_1">'.$data['subject_name'].'</label></td>'; $count++; //Increment the count } } } echo '</tr></table><br/>'; //Close your last row and your table, outside the loop ?> This is the end result of the HTML code, through 'view page source' in web browser Code: [Select] <input name="level[]" type="checkbox" id="level_1" value="1"> <span class="zone_text_enlarge"><label for="level_1">Pre-School</label></span><br/> <table><tr></tr> <tr> <td><input name="subject_level[]" class="subject_a" type="checkbox" checked="checked" id="subject_level_1" value="1"/> <label for="subject_level_1" class="subject_1">Mathematics</label></td> <td><input name="subject_level[]" class="subject_a" type="checkbox" id="subject_level_1" value="1"/> <label for="subject_level_1" class="subject_1">Mathematics</label></td> <td><input name="subject_level[]" class="subject_a" type="checkbox" id="subject_level_2" value="2"/> <label for="subject_level_2" class="subject_1">English</label></td> <td><input name="subject_level[]" class="subject_a" type="checkbox" id="subject_level_2" value="2"/> <label for="subject_level_2" class="subject_1">English</label></td> <td><input name="subject_level[]" class="subject_a" type="checkbox" id="subject_level_3" value="3"/> <label for="subject_level_3" class="subject_1">Chinese</label></td> <td><input name="subject_level[]" class="subject_a" type="checkbox" id="subject_level_3" value="3"/> <label for="subject_level_3" class="subject_1">Chinese</label></td> <td><input name="subject_level[]" class="subject_a" type="checkbox" id="subject_level_4" value="4"/> <label for="subject_level_4" class="subject_1">Tamil</label></td> <td><input name="subject_level[]" class="subject_a" type="checkbox" id="subject_level_4" value="4"/> <label for="subject_level_4" class="subject_1">Tamil</label></td> <td><input name="subject_level[]" class="subject_a" type="checkbox" id="subject_level_5" value="5"/> <label for="subject_level_5" class="subject_1">Malay</label></td> <td><input name="subject_level[]" class="subject_a" type="checkbox" id="subject_level_5" value="5"/> <label for="subject_level_5" class="subject_1">Malay</label></td> </tr> <tr> <td><input name="subject_level[]" class="subject_a" type="checkbox" id="subject_level_6" value="6"/> <label for="subject_level_6" class="subject_1">Phonics</label></td> <td><input name="subject_level[]" class="subject_a" type="checkbox" checked="checked" id="subject_level_6" value="6"/> <label for="subject_level_6" class="subject_1">Phonics</label></td> <td><input name="subject_level[]" class="subject_a" type="checkbox" id="subject_level_7" value="7"/> <label for="subject_level_7" class="subject_1">Creative Writing</label></td> <td><input name="subject_level[]" class="subject_a" type="checkbox" id="subject_level_7" value="7"/> <label for="subject_level_7" class="subject_1">Creative Writing</label></td></tr> </table><br/> Hi, Right when a form is submit the data in the form is added to the database basically I need to know how I can check one of the inputs to see if 'http://' has been add along with an URL, if it has not I need to add it in front of the string. How can this be done? Regards Lee Hi, I have a question. Currently, when I retrieve the records in my zone 'North', there is no misalignment. However when I tried to copy the same coding to make up zone 'West', the alignment went off? May I know what is the reason? Any advice to align it properly. Attached is the picture of my example <div id ="location"> <input name="zone[]" type="checkbox" id="1" value="1"> <label for="1">North</label><br /> <?php $dbc = mysqli_connect('localhost', 'aliendatabase', '1234567', 'aliendatabase') or die(mysqli_error()); $query = ("SELECT * FROM tutor_preferred_district ORDER BY district_id ASC"); $sql = mysqli_query($dbc, $query) or die(mysqli_error()); echo'<table><tr>'; // Start your table outside the loop... and your first row $count = 0; // Start your counter while($data = mysqli_fetch_array($sql)) { /* Check to see whether or not this is a *new* row If it is, then end the previous and start the next and restart the counter. */ if ($count % 3 == 0) { echo "</tr><tr>"; $count = 0; } echo '<td><input name="district[]" type="checkbox" id="'.$data['district_id'].'" value="'.$data['district_id'].'">'; echo '<label for="'.$data['district_id'].'">'.$data['district_name'].'</label></td>'; $count++; //Increment the count } echo '</tr></table><br />'; //Close your last row and your table, outside the loop ?> <input name="zone[]" type="checkbox" id="2" value="2"> <label for="2">West</label><br /> <?php $dbc = mysqli_connect('localhost', 'aliendatabase', '1234567', 'aliendatabase') or die(mysqli_error()); $query = ("SELECT * FROM tutor_preferred_district ORDER BY district_id ASC"); $sql = mysqli_query($dbc, $query) or die(mysqli_error()); echo'<table><tr>'; // Start your table outside the loop... and your first row $count = 0; // Start your counter while($data = mysqli_fetch_array($sql)) { /* Check to see whether or not this is a *new* row If it is, then end the previous and start the next and restart the counter. */ if ($count % 3 == 0) { echo "</tr><tr>"; $count = 0; } echo '<td><input name="district[]" type="checkbox" id="'.$data['district_id'].'" value="'.$data['district_id'].'">'; echo '<label for="'.$data['district_id'].'">'.$data['district_name'].'</label></td>'; $count++; //Increment the count } echo '</tr></table>'; //Close your last row and your table, outside the loop ?> </div> I am assuming doing the following is bad practice as the short name "Account" is both for the repository and account. Agree? Appears that use always takes precedent over namespace and just Account\User is always short for \NotionCommotion\Domain\Entity\Account\User and not \NotionCommotion\Api\Account\User. Still sees like it could easily result in confusion. namespace NotionCommotion\Domain\Repository\Account; use NotionCommotion\Domain\Entity\Account; class AccountRepository { public function getUser(string $mainKey, int $userId):?Account\User { // } } Would it be better to do something like the following? Interestingly, it worked fine on the below class but when I tried to do so on the above AccountRepository class, it resulted in AccountDir\User class not found error. Is there any logic reason why that might/should happen, or is it more likely I just have some error elsewhere? namespace NotionCommotion\Api\Account; use NotionCommotion\Domain\Entity\Account as AccountDir; class AccountService { public function read():AccountDir\Account { // } } Hi have a webpage (homepage) that has a wordpress blog linked to it. when i save the page exactly as it is for example. home.php then saves as home1.php and load the home1.php the wordpress blog is missing. i dont use wordpress normally this is a site i have been sent to amend some part in php. Is the reason for this wordpress backend is pointing to the home.php?
here is the code for the blog
<div class="blog_container"> <a href="/blog" class="title_link">Latest Blog Posts</a> <? if ($posts) { ?> <ul> <? foreach ($posts as $post) { ?> <li> <a href="<?= $post->guid ?>"> <?= word_limiter($post->post_title, 10) ?> <span><?= date('d-m-Y', strtotime($post->post_date)) ?></span> <strong>read more ></strong> </a> </li> <? } ?> </ul> <? } ?> </div>thanks Hello, guys. I'm using the following code to send me an automated mail in case the user find a broken link. But there are 2 problems: 1) after submitting I'd rather not disrupt their game by refreshing the page. So, is there any way to send mail without the post refreshing? 2) if the user hits F5, the page reloads and the code runs again, sending two or more times the same email. How can I avoid duplicated emails caused by manual browser refresh? (the most important solution currently is how to to avoid duplicated entries, in case an ajax solution is too hard to post here.) <form name="fbrlink" action="<?= $_SERVER['PHP_SELF']."?page=ok" ?>" method="post"> <?php echo('<input name="sbrlink" id="B1" type="button" value="broken Link?" style="border:none; background-color:#FFFFFF; font-family:Trebuchet MS; font-size:15px; font-weight:bold; color:#1A4877; cursor:pointer; display:block; text-decoration:underline;" onclick="this.form.submit();"/>'); ?> <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($_SERVER['QUERY_STRING'] == 'page=ok') { echo '<script type="text/javascript" >confirm("Click OK to notify us.");</script>'; mail("contact@ctt.net","report broken link","Game notified:MarioBros"); echo '<script type="text/javascript" >document.fbrlink.sbrlink.disabled = true;</script>'; ?> <script type="text/javascript" >document.fbrlink.action='<?php echo $a; ?>'; alert('<?php $_SERVER['QUERY_STRING'];?>'); </script> <?php } else {} }else{ } ?> For solely to reduce duplicated code and when injection isn't applicable, should one use inheritance or traits? <?php abstract class BaseClass { private $id, $name; public function __construct(int $id, string $name) { $this->id=$id; $this->name=$name; } public function getId():int { return $this->id; } public function getName():string { return $this->name; } } <?php class A extends BaseClass{} class B extends BaseClass{} class C extends BaseClass { private $foo; public function __construct(int $id, string $name, Foo $foo) { parent::__construct($id, $name); $this->foo=$foo; } public function getFoo():Foo { return $this->foo; } } <?php trait MyTrait { private $id, $name; public function __construct(int $id, string $name) { $this->id=$id; $this->name=$name; } public function getId():int { return $this->id; } public function getName():string { return $this->name; } } <?php class A { use MyTrait; } class B { use MyTrait; } class C { use MyTrait; private $foo; public function __construct(int $id, string $name, Foo $foo) { $this->id=$id; $this->name=$name; $this->foo=$foo; } public function getFoo():Foo { return $this->foo; } } Edited January 2, 2020 by NotionCommotion Okay, after successfully getting a form to input data to a MySQL database, I'm now trying to get the data back out by searching. Search form code: Code: [Select] <form name="search" method="post" action="process_search.php"> Seach for: <input type="text" name="find" /> in <select name="columns"> <option value="">Please choose one:</option> <option value="status">Status</option> <option value="date">Date</option> <option value="species">Species</option> <option value="breed">Breed</option> <option value="sex">Sex</option> <option value="primary_colour">Primary Colour</option> <option value="colour">Colour</option> <option value="distinctive_traits">Distinctive Traits</option> <option value="fur_length">Fur Length</option> <option value="age">Age</option> <option value="desexed">Desexed</option> <option value="microchipped">Microchipped</option> <option value="suburb">Suburb</option> <option value="pound_area">Pound Area</option> <option value="contact">Contact</option> <option value="link">Link</option> </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> All good there! Form displays how I want it to, and submits correctly. The bit I'm struggling with is the process_search.php side of things. This is what I have (note that i'm trying to get the results into a formatted table and I'd like more results if the match is in more than one column): Code: [Select] <? //Here we display stuff if they have submitted the form if ($searching =="yes") { echo "<h2>Results</h2><p>"; //If they stuffed up and didn't search for anything, we show them this if ($find == "") { echo "<p>Oh, Bianca, you need to enter SOMETHING to search!"; exit; } //If everything is all good, we connect to the database mysql_connect("localhost", "******", "******") or die(mysql_error()); mysql_select_db("******") or die(mysql_error()); //Let's not forget the register globals off crap $status = $_POST['status']; $date = $_POST['date']; $species = $_POST['species']; $breed = $_POST['breed']; $sex = $_POST['sex']; $primary_colour = $_POST['primary_colour']; $colour = $_POST['colour']; $distinctive_traits = $_POST['distinctive_traits']; $fur_length = $_POST['fur_length']; $age = $_POST['age']; $desexed = $_POST['desexed']; $microchipped = $_POST['microchipped']; $suburb = $_POST['suburb']; $pound_area = $_POST['pound_area']; $contact = $_POST['contact']; $link = $_POST['link']; $columns = $_POST['columns']; // We perform a bit of filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); //Now we search for our search term, in the field the user specified $data = mysql_query("SELECT * FROM animal_info WHERE upper($columns) LIKE'%$find%'"); //And we display the results where($result = mysql_fetch_array( $data )) { echo "<tr>"; echo "<td>".$row['status']."</td>"; echo "<td>".$row['date']."</td>"; echo "<td>".$row['species']."</td>"; echo "<td>".$row['breed']."</td>"; echo "<td>".$row['sex']."</td>"; echo "<td>".$row['primary_colour']."</td>"; echo "<td>".$row['colour']."</td>"; echo "<td>".$row['distinctive_traits']."</td>"; echo "<td>".$row['fur_length']."</td>"; echo "<td>".$row['age']."</td>"; echo "<td>".$row['desexed']."</td>"; echo "<td>".$row['microchipped']."</td>"; echo "<td>".$row['suburb']."</td>"; echo "<td>".$row['pound_area']."</td>"; echo "<td>".$row['contact']."</td>"; echo "<td>".$row['link']."</td>"; echo "</tr>"; } else { echo "ERROR: ".mysql_error(); } //This counts the number or results - and if there wasn't any it gives them a little message explaining that $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "Nope, couldn't find anything here! Maybe refine your search criteria?<br><br>"; } //And we remind them what they searched for echo "<b>Searched For:</b> " .$find; } ?> Trouble with this is that I'm not getting any errors OR results! ANY help would be appreciated HUGELY! Cheers, Dave I am testing live and the data is downloading to the database but when it goes to the next page it gets this message. Warning: Wrong parameter count for mysql_result() in /home/ebermy5/public_html/html/login.php on line 25 Could not execute query THIS IS MY CODING ANY IDEAS WHERE I AM GOING WRONG. YES I HAVE THE OPEN AND CLOSE PHP? if (@$_SESSION['auth'] != "yes") @include('Connections/connect_to_mysql.php'); $query = "SELECT firstName, lastName FROM `Members` WHERE email='{$_SESSION['id']}'"; $result = mysql_result($query) or die("Could not execute query"); echo "<html> <head><title>New Member Welcome</title></head> <body> <h2 style='margin-top: .7in; text-align: center'> Welcome $firstName </h2>\n"; hi all, What I expect: - The function to loop output all of the rows which match the search results. The problems: - Only 1 row is matched. - Notice: Undefined index: id in G:\xampp\htdocs\xampp\dsa\search_func.php on line 57 - The image is not showing Line 57: Code: [Select] $id = $_REQUEST['id']; Entire Code: Code: [Select] <?php /* return the details of an employee parameter empno - employee number eg empHTML.php?empno=7521 This is very basic e.g no error checking */ function search($dbc, $id) { if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) { $terms = $_GET['terms']; $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server'); $query = "SELECT * FROM stick, location, identification WHERE make LIKE '%$terms%' AND stick.sid = location.lid AND identification.stickID = stick.sid AND identification.stickID = $id"; $result=mysqli_query($dbc,$query); $num_rows = mysqli_num_rows($result); $output = Array(); $image = mysqli_fetch_object($result); if ($num_rows > 0){ if($result=mysqli_query($dbc,$query)){ $output[] = '<ul>'; $output[] = '<li> Type: '.$image->make .'<br />Size: '.$image->size .$image->type .'<br />Colour: '.$image->colour . '<br /> Street Missing in: ' .$image->street.'<br />Town missing in: '.$image->town .'<br />City missing in: '.$image->city.'</li>'; $output[] = '</ul>'; } return join('',$output); } else { echo "<h3>Sorry,</h3>"; echo "<p>your search: "" .$terms. "" returned zero results</p>"; } } else { // Tell them to use the search form. echo '<p class="error">Please use the search form at the top of the window to search this site.</p>'; } } // connect $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); // get the id from the URL $id = $_REQUEST['id']; print search($dbc, $id); print "<img src='getPhoto.php?id=$id'/>"; // close the database mysqli_close($dbc); Hi, this is how I echo some results from my DB: Code: [Select] <?php if(mysql_num_rows($execute)>0){ do{ echo " " .$result['id_cliente']. " " .$result['company_name']. " " .$result['cliente_tel']. " " ; }while($result = mysql_fetch_assoc($execute)); }else{ echo " No customer found. " ; } ?> and this echoes the results in one paragraph. I need to print the results in rows...and I need help with that. :shy:Thanks I have a variable called clientid being passed via a url. I place that in a variable called $clientid I have two tables, client and web_info I want to select all from client and web_info for the appropriate clientid. I may not have my tables set up correctly. The primary key on the table client is auto genereated and titled client_id the primary key on web_info is e_mail and I want to pull information from the table web_info where the e_mail columns match. Below is my PHP: $query = "SELECT * FROM client WHERE $clientid = client_id and FROM web_info WHERE client.e_mail = web_info.e_mail"; $result = mysqli_query($connection,$query); $row = mysqli_fetch_array($result); I know I probably screwed all this up but please help hello, is there a wat to limit the following to only the first 25 characters? $customername=$row1["customer"]; thanks I have an SQL table such as this: Pairing1 Pairing2 Week 6 1 1 5 2 1 4 3 1 1 3 2 5 4 2 3 6 2 and another with teams such as: teamid teamname 1 Teamname 1 2 Teamname 2 3 Teamname 3 The numbers in the pairing are numbers of teams, which is in another SQL table. What would be the most efficient way... of displaying the information in the form of pairngs such as Week 1: Team 6 vs Team 1 Team 5 vs Team 2 etc .... What is the best way for me to sort my results? Hello dear friends, I've made littel code that fetch the categories name from an software website $text= file_get_contents('http://win.softpedia.com/'); // it make win.softpedia as text code $start = strstr("$text",'<ul id="windows_cat">'); // search inside to that start point $end = strpos("$start",'" title="'); // end at it to that end point $cat = substr($start,66,$end-66); echo "$cat<hr>"; it will show now the categories but it only shows the 1st one Antivirus/ how then loop it to find next and next and so on to show me the list of the categories ! thanks |