I have a SQL query statement using PHP that produces the following result:
NAME FOODS
Ben Pizza
Ben Fries
Ben Choco
Kelly Fries
Chit Pizza
But what I want to view is like this:
NAME FOODS
Ben Pizza
___ Fries
___ Choco
Kelly Fries
Chit Pizza
*Replace the underscores with spaces
Just want to get rid the redundancy. Please help to come up with an SQL statement or PHP codes. Thank you.
Hereās a quick āstabā at it.
<?php
$array = array(
array(
'name' => 'Ben',
'food' => 'Pizza',
),
array(
'name' => 'Ben',
'food' => 'Fries',
),
array(
'name' => 'Ben',
'food' => 'Choco',
),
array(
'name' => 'Kelly',
'food' => 'Fries',
),
array(
'name' => 'Chit',
'food' => 'Pizza',
),
);
class SomethingIterator extends ArrayIterator{
protected
$previousName = null;
public function current(){
$current = parent::current();
if($this->previousName === $current['name']){
$current['name'] = '';
}else{
$this->previousName = $current['name'];
}
return $current;
}
}
foreach(new SomethingIterator($array) as $user){
print_r($user);
}
/*
Array
(
[name] => Ben
[food] => Pizza
)
Array
(
[name] =>
[food] => Fries
)
Array
(
[name] =>
[food] => Choco
)
Array
(
[name] => Kelly
[food] => Fries
)
Array
(
[name] => Chit
[food] => Pizza
)
*/
?>
I used a sql statement:
$sql = āSELECT name, foods FROM peopleā
$result=mysql_query($sql);
and inside the <td>
<td>
<?php
//Header in the .xls file
$_SESSION[āreport_headerā]=array(āNameā,āFoodsā);
//Table header
echo "<table id='box-table-a'>";
echo "<thead><tr><th scope='col'>Name</th><th scope='col'>Foods</th>
</tr></thead>";
$i=0;
//Generate data from query
while($row = mysql_fetch_array($result))
{
echo "<tbody><tr><td>";
echo $_SESSION['report_values'][$i][0]=$row['0'];
echo "</td><td>";
echo $_SESSION['report_values'][$i][1]=$row['1'];
echo "</td></tr></tbody>";
$i++;
}
echo "</table>";
//Close database
mysqli_close($con);
?>
</td>
r937
November 3, 2010, 9:54am
4
to accomplish what you want in the php, you must have an ORDER BY clause in your query
Itāll just rearrange the names but not delete the redundant names in view using PHP.
ORDER BY will just result in:
Ben Pizza
Ben Fries
Ben Coke
Mary Pizza
Not:
Ben Pizza
(space) Fries
(space Coke
Mary Pizza
r937
November 5, 2010, 1:46am
6
actually, you ~do~ need ORDER BY
otherwise you could get this ā
Ben Pizza
Mary Pizza
Ben Fries
Ben Coke
and that wonāt look right after being processed by php ā
Ben Pizza
Mary Pizza
Ben Fries
____ Coke
r937:
actually, you ~do~ need ORDER BY
otherwise you could get this ā
Ben Pizza
Mary Pizza
Ben Fries
Ben Coke
and that wonāt look right after being processed by php ā
Ben Pizza
Mary Pizza
Ben Fries
____ Coke
Yeah. After Order By? Whatās next to get rid the redundant?
the trick isnt to get rid of the redundant inside the query, itās to ignore the redundant when youāre outputting.
Store the ācurrentā personās name as a variable, and as youāre walking through the results, only display the name when it changes.
StarLion:
the trick isnt to get rid of the redundant inside the query, itās to ignore the redundant when youāre outputting.
Store the ācurrentā personās name as a variable, and as youāre walking through the results, only display the name when it changes.
From what you told, I added some codes:
//Header in the .xls file
$_SESSION['report_header']=array("Name","Foods");
//Table header
echo "<table id='box-table-a'>";
echo "<thead><tr><th scope='col'>Name</th><th scope='col'>Foods</th>
</tr></thead>";
$i=0;
//Generate data from query
while($row = mysql_fetch_array($result))
{
echo "<tbody><tr><td>";
$namep = $row['0'];
$_SESSION['report_values'][$i][0]=$namep;
if ($namep=$namep) {
echo "";
} else {
echo $namep;
}
echo "</td><td>";
echo $_SESSION['report_values'][$i][1]=$row['1'];
echo "</td></tr></tbody>";
$i++;
}
echo "</table>";
But it canāt seem right because the Name didnāt show at all now.
help
if ($namep=$namep) {
does this not strike you as wrong?
if (TheStoredName != TheCurrentRowsName) {
TheStoredName = TheCurrentRowsName;
echo TheCurrentRowsName;
}
leikeze
November 8, 2010, 1:42am
11
sorry. iām not good in programming. actually, the most hated subject. haha. kidding! itās just that iām not good on it. (: just doing it for a project⦠T_T sigh XD
leikeze
November 8, 2010, 1:46am
12
Where will i get the variable TheStoredName and TheCurrentRowsName?
The row[ānameā] will be passed into a variable like $TheStoredNameā¦
How about the TheCurrentRowsName? row[ānameā] too?
May I know the code construction? :x
sowee. really donāt know anything about codings⦠:sick:
TheCurrentRowsName = $row[ānameā]
TheStoredName = another variable; could be anything.
Basically what the code does is that if the stored name is not the same as the current name (IE: Whenever it changes), it outputs the new personās name, and then sets the stored name as the current one, so that the next time through the loop, if the name hasnt changed, it doesnt print out the name.