Need help removing duplicate columns in a mysql result - php

I have a website that returns results by country. There is a query that returns this, then I use a loop to output the data. The only problem is that for one of these, there is a sub-50 column and the results are the same all the way to the sub-36 column. I need help removing the duplicate columns.
// By country
// sort countries by most subXs in descending order
arsort($country_sub{$dsub});
// print results
echo "<b><span style='font-size:20px;'>By Country</span></b>";
echo "<table><tr style='font-weight:bold;'><td style='width:100px;'>Country</td><td>sub".$dsub."</td>";
for($i=$dsub-1; $i>=$x; $i--){ echo "<td>sub".$i."</td>"; }
echo "</tr>";
foreach($country_sub{$dsub} as $country => $value){
echo "<tr><td>".$country."</td><td>".$value."</td>";
for($i=$dsub-1; $i>=$x; $i--){
if (isset($country_sub{$i}[$country])) {
echo "<td>".$country_sub{$i}[$country]."</td>";
} else{
echo "<td></td>";
}
echo "</tr>";
}
echo "</table><br/>";
The array $country_sub{$dsub} contains the number of people with that result or less. The foreach loop is what outputs the top numbers, and the for loop is what outputs the results itself.
You can see where this code is used at this link: http://cubingstats.netau.net/3bld/index.php. It's used in the "By Country" section. All I want is in that table, to show sub 36 and sub 27 (the ones listed above). Any help is extremely useful!
EDIT: Lines 26-32 (see comments below for link) show the addOne function, which is what adds it to the array. This is then used on lines 48-50 and 68-70 as the results are displayed.

In your SQL query, add DISTINCT just after the SELECT
SELECT DISTINCT fields,fields...fields
FROM...

Related

array_intersect and match multiple values same time

In this script, you can see i try validate if 2 values - Jhon 34 -there are in the string called $values the same time, when i send the search i use 2 o 3 words and the idea it´s verification if find exactly this 3 or 2 words, etc, inside array
<?php
$values="Jhon,Smith,252546,34,house,car,phone";
$post="Jhon 34";
$exp_values=explode(",",$values);
$exp_post=explode(" ",$post);
$result=array_intersect($exp_post,$exp_values);
foreach ($result as $results) {
if(count($result)==count($exp_post)) {
echo $results;
print "<br>";
}
}
?>
I use count for show result only if the intersect elements it´s the same number as in the $post, because $post show values i want search inside $values, the result it´s ok if the same words find inside $values
The results it´s wrong because detect one word but i need detect all words i send, if array have all these words result must be ok, if haven´t this result it´s bad
You have the if and foreach backwards. First check if the count is the same to know that the post is valid, then show the results.
And instead of a loop, you can simply implode() $result:
if (count($result) == count($exp_post)) {
echo implode('<br>', $result);
} else {
echo "Invalid input";
}

Ho to print PDO results in a while loop but from a user specified index and not from start?

I have a database result set created from a PDO object.
The array contains all the data. Now I want to print the results but I want to start the printing from a specific index and not from the start.The start index is specified from a user. Please don't tell me to modify the query because this is not what I want. Also I've searched everywhere and didn't found any solutions to this.
I simplified my code so it's more understandable and easier to come to the point.
Thanks for any-kind of help. :>)
$res2=$conn->prepare("SELECT COUNT(*) FROM blogs");
$res2->execute();
while($r=$res2->fetch(PDO::FETCH_BOTH)){
// I have 37 records in $res2 and want to start echoing from record number 10.
//for example I want to echo out $r['title'] but not from the first but from the 5th or 10th index.
}
Use an if() statement and a counter:
$userInput = 5;
$i = 0;
while($r=$res2->fetch(PDO::FETCH_BOTH)){
if($i >= $userInput){
// echo your output here
}
$i++;
}

Get out of loop

I have such rows in my table in MySQL ( I wont post all the data which is in "table1", because I use only this) :
From this table I get each value with foreach loop and group it by "time_from" ascending order, because I need to start from the lowest one.
With echo I print such table in html :
I add the values to the table by checking the "time_from" database with those values that are printed with for loop. The problem is that when I don't know how to stop the loop at the exact time so it should start not from the beginning but rowwhere it stopped.
foreach ($mydbdata as $row) { //only as example,because I get all the data normally,checked everything with echo to get sure.
for($time = 0; $time <= 24; $time++)
{
if($row['time_from'] == $time) {
echo '<td>OK</td>';
}
else {'<td>BAD</td>';}
}
}
The problem is that it prints all the row, adds data corresponding to if clause, but when it goes to second check it prints again all the cells in the same row. I need somehow to stop the loop and get back,but start not from the start, but from that value on which it stopped. Any ideas how to do that? I have tried with "break;" but anyway, it is not starting from the place I need.

Print multiple arrays into html table

I have two arrays built from an XML response to an api. One array gives me the product info form one store and the other array gives me just the stock level in another. It looks like this:
foreach($filteredStock as $t=>$k){
$codeFirst[] = $k['code'];
echo '<tr><td>'.$t;
echo '</td><td>'.$k['desc'];
echo '</td><td>'.$k['family'];
echo '</td><td>'.$_POST['filterSelect'];
echo '</td><td>'.$k['onOrder'];
echo '</td><td>'.$k['cost'];
echo '</td><td>'.$k['sell'];
echo '</td><td>'.$k['invStore'];
echo '</td>';
}
$output = array();
$result = array_intersect_key($mainArray, array_flip($codeFirst));
foreach($result as $results=>$rValues){
echo '<td>'.$rValues['inv'];
echo '</td>';
echo '</tr>';
}
I want to display it so that it looks like this in the table:
code / desc / family / filteredClass / onOrder / cost / sell / invStore / invwarehouse
Because I need the second foreach loop to grab the values of the second array it causes a problem trying to get it to repeat on each line with the other array. Any suggestions?
Rather than echoing output each time the loop iterates, why don't you build up the table how you want it and then output it when you have it arranged properly?
It's not clear to me from the code exactly what you are trying to do, but if it's an organisation problem then why don't you store the values in seperate arrays and then combine then and arrange them appropriately before outputting them?
Using echo immediately for each loop iteration limits your options somewhat.

Show db row with a specific not null column in foreach loop before others are displayed

I have a listings table, and each listing has column called 'top'. Basically, if top is not null, I want that listing to show up first, then the others.
Right now, my code is like this:
foreach ($results as $result):
echo $result->name;
endforeach;
This will show me everything, but I want the rows with 'top' not null to show up first. What's the best way to go about this?
Can't you apply an ORDER BY to the query that feeds $results?
I agree with Aaron... the best thing to do is to order them so that they are first in the query.
If for some reason you couldn't do that you could do a few other things:
Sort the object or array in PHP... may have to use a custom function .. if that's the case see: Sort Object in PHP
Grab the rows that only have the NULL first, print them, then grab the ones that aren't null.
You could loop through the array twice. First only printing the NULLs, then again to print the others.
Try
foreach ($result as $row){
Then specify each element you want in order.
echo $row['top']
echo $row['your next data row']...
and have them space exactly how you want.
And if you need it formated, try using <pre> first.
You can also add a if statment.
$data = $row['top']
if($top == 1) { then echo this } else {echo the values that are null }
Based on your comment below #Aaron Bertrand's answer, you would need an order clause in your sql like:
ORDER BY ISNULL(`top_ad`), the_rest_of_your_order_clause

Categories