I've got a query which outputs the following data:
Treatment ID --------- Treatment_Name ---------- Customer_type
1 Treatment 1 Ladies
1 Treatment 1 Mens
3 Treatment 2 Ladies
4 Treatment 3 Mens
I need to loop over this and output a similar structure to this:
<div class="mens ladies"> <!-- these are read from the customer_type column -->
<h4>Treatment 1</h4> <!-- this is read from the treatment_name column -->
</div>
So basically grouping by the Treatment name (or ID) and then outputting all of the customers related to this treatment.
Coming from a ColdFusion background I assumed this would be easy with a Query of Queries etc. however I've struggled with coming up with a PHP solution.
I've tried to do a while loop and only show data if the treatment_name has changed which seems to work fine but I'm not sure how to display the customer_types related to this.
Any ideas? Thanks.
Edit
Here is a copy of the query returned from the db using a mysqli_fetch_all:
array (size=22)
0 =>
array (size=3)
0 => string 'Mens' (length=4)
1 => string '1' (length=1)
2 => string 'treatment 1' (length=11)
1 =>
array (size=3)
0 => string 'Ladies' (length=6)
1 => string '1' (length=1)
2 => string 'treatment 1' (length=11)
2 =>
array (size=3)
0 => string 'Kids' (length=4)
1 => string '2' (length=1)
2 => string 'treatment 2' (length=11)
3 =>
array (size=3)
0 => string 'Ladies' (length=6)
1 => string '2' (length=1)
2 => string 'treatment 2' (length=11)
4 =>
array (size=3)
0 => string 'Mens' (length=4)
1 => string '2' (length=1)
2 => string 'treatment 2' (length=11)
5 =>
array (size=3)
0 => string 'Mens' (length=4)
1 => string '3' (length=1)
2 => string 'treatment 3' (length=11)
6 =>
array (size=3)
0 => string 'Kids' (length=4)
1 => string '3' (length=1)
2 => string 'treatment 3' (length=11)
7 =>
array (size=3)
0 => string 'Ladies' (length=6)
1 => string '3' (length=1)
2 => string 'treatment 3' (length=11)
8 =>
array (size=3)
0 => string 'Ladies' (length=6)
1 => string '4' (length=1)
2 => string 'treatment 4' (length=11)
It continues in the same format...
Hope this answers your question:
<?php
$link = mysqli_connect("hostname","username","password","database","port") or die("Error " . mysqli_error($link));
$query = "SELECT treatment_name,Customer_type from Treatments" or die("Error at.." . mysqli_error($link));
$result = $link->query($query);
while($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
//I have used this sample array: Try and make your result set this way
$data = array(
array('treatment_name'=>'Treatment1','Customer_type'=>'Mens'),
array('treatment_name'=>'Treatment1','Customer_type'=>'Ladies'),
array('treatment_name'=>'Treatment2','Customer_type'=>'Mens')
);
foreach ($data as $key => &$value) {
$temp[$value['treatment_name']][$key] = $value['Customer_type'];
}
//echo "<pre>";
//print_r($data);
//print_r($temp);
//die;
foreach($temp as $key => $value){
echo "<div class = \"";
foreach($value as $key2 => $value2)
echo " ". $value2. " ";
echo "\"><h4>".$key."</h4></div></br>";
}
?>
//Output
<div class = " Mens Ladies "><h4>Treatment1</h4></div></br><div class = " Mens "><h4>Treatment2</h4></div></br>
You can use the commented lines for checking the data and adjusting accordingly.
By the way, this code directly prints the output as html, so your browser will render it. Right click the page and view source if you want the content!
Related
I tried mutiple LEFT JOIN in mysql it works fine there but however if use the same query in php mysqli object method I am not getting what I get in direct mysql
HERE is the Query in MYSQL
SELECT f.*,o.*,fs.* FROM fruits f
LEFT JOIN orders o ON f.id = o.fruit_id
LEFT JOIN fruit_stock fs ON f.id = fs.f_id
MYSQL RESULT
id name price id fruit_id qty id f_id stock_qty
3 Banana 5 2 3 10 1 3 122
3 Banana 5 4 3 8 1 3 122
2 Apple 3 1 2 3 2 2 322
4 pomegranate 4 3 4 15 3 4 23
5 grape 3 NULLNULL NULL 4 5 12
1 mango 45 NULLNULL NULL NULLNULL NULL
Same query with php
$con1 = new mysqli('***','***','***','***');
$sel_sql = 'SELECT f.*,o.*,fs.* FROM fruits f LEFT JOIN orders o ON f.id = o.fruit_id LEFT JOIN fruit_stock fs ON f.id = fs.f_id';
$result = $con1->query($sel_sql);
var_dump($result);
while($row = $result->fetch_assoc()){
var_dump($row);
}
I just var_dump the $row to see the result. When I see I am not getting id for the 6th row as seen on mysql instead I get null
array (size=7)
'id' => string '1' (length=1)
'name' => string 'Banana' (length=6)
'price' => string '5' (length=1)
'fruit_id' => string '3' (length=1)
'qty' => string '10' (length=2)
'f_id' => string '3' (length=1)
'stock_qty' => string '122' (length=3)
array (size=7)
'id' => string '1' (length=1)
'name' => string 'Banana' (length=6)
'price' => string '5' (length=1)
'fruit_id' => string '3' (length=1)
'qty' => string '8' (length=1)
'f_id' => string '3' (length=1)
'stock_qty' => string '122' (length=3)
array (size=7)
'id' => string '2' (length=1)
'name' => string 'Apple' (length=5)
'price' => string '3' (length=1)
'fruit_id' => string '2' (length=1)
'qty' => string '3' (length=1)
'f_id' => string '2' (length=1)
'stock_qty' => string '322' (length=3)
array (size=7)
'id' => string '3' (length=1)
'name' => string 'pomegranate' (length=11)
'price' => string '4' (length=1)
'fruit_id' => string '4' (length=1)
'qty' => string '15' (length=2)
'f_id' => string '4' (length=1)
'stock_qty' => string '23' (length=2)
array (size=7)
'id' => string '4' (length=1)
'name' => string 'grape' (length=5)
'price' => string '3' (length=1)
'fruit_id' => null
'qty' => null
'f_id' => string '5' (length=1)
'stock_qty' => string '12' (length=2)
array (size=7)
'id' => null
'name' => string 'mango' (length=4)
'price' => string '45' (length=2)
'fruit_id' => null
'qty' => null
'f_id' => null
'stock_qty' => null
I am expecting to get the id from the fruit table(which is 1 and the fruit name is mango) but its returning null.
Not sure why is that happening. Any help?
fruit_stock TABLE
id f_id stock_qty
1 3 122
2 2 322
3 4 23
4 5 12
orders Table
id fruit_id qty
1 2 3
2 3 10
3 4 15
4 3 8
I don't want to care about id in other table I just want the main(fruits) table id and other corresponding whole data from other tables.
You can add alias to your ids or remove the useless ones (optional ones are commented):
$sel_sql = 'SELECT
f.*,
-- o.id as order_id,
o.fruit_id,
o.qty,
-- fs.id as fruit_stock_id,
fs.f_id,
fs.stock_id
FROM fruits f LEFT JOIN orders o ON f.id = o.fruit_id LEFT JOIN fruit_stock fs ON f.id = fs.f_id';
Then you will have only one index id on your array.
You can only have one key with a value of Id so when the query runs it is first 1 and then when it hits the 2nd Id it overwrites it with null note that you only have 1 Id in your array but two in the query. You will need to be a bit more selective in the query and alias some of the columns.
Let's say I have the following query, wich gives me the amount of coupons downloaded and redeemed, grouped by date: [EDIT](I can't modify this query)[/EDIT]
$sql = "SELECT DATE(datetime) ,
SUM(CASE WHEN datetime!='' THEN 1 ELSE 0 END) As downloaded ,
SUM(CASE WHEN used = 1 AND DATE(datetime) != '' THEN 1 ELSE 0 END) AS redeemed
FROM Promotion_Redeem
WHERE datetime BETWEEN '2013-10-01' AND '2013-10-04'
GROUP BY DATE(datetime)";
How can I get the sum of downloaded and the sum of redeemed? this should be within $sql, somewhere... below is the result for this query:
row number 1
array (size=3)
0 => string '2013-10-01' (length=10)
1 => string '126' (length=3)
2 => string '11' (length=2)
row number 2
array (size=3)
0 => string '2013-10-02' (length=10)
1 => string '106' (length=3)
2 => string '5' (length=1)
row number 3
array (size=3)
0 => string '2013-10-03' (length=10)
1 => string '228' (length=3)
2 => string '12' (length=2)
row number 4
array (size=3)
0 => string '2013-10-04' (length=10)
1 => string '149' (length=3)
2 => string '9' (length=1)
[EDIT]bove you can see I get 4 rows, each one whith an array... I want, for instance, the sum of the third field from each of these arrays... In my example, this would equals to 37 (that means, 37 coupons redeemed)[/EDIT]
I got this data structure after using this:
while ($row = mysql_fetch_row($result)) {
echo "row number ".++$i;
var_dump($row);
}
Thanks in advance
Modify your php loop like so:
$downloaded=0;
$redeemed=0;
while ($row = mysql_fetch_row($result)) {
echo "row number ".++$i;
$downloaded+=$row[1];
$redeemed+=$row[2];
var_dump($row);
}
echo "Downloaded: $downloaded<br>";
echo "Redeemed: $redeemed<br>";
I have an array that looks like this:
array (size=21)
0 => string '2' (length=1)
1 => string '' (length=0)
2 => string '20' (length=2)
3 => string '19' (length=2)
4 => string '14' (length=2)
5 => string '13' (length=2)
6 => string '' (length=0)
7 => null
8 => null
9 => string '20' (length=2)
10 => null
11 => string '10' (length=2)
12 => string '' (length=0)
13 => null
14 => string '13' (length=2)
15 => null
16 => string '' (length=0)
17 => null
18 => null
19 => string '' (length=0)
20 => string '20' (length=2)
And I would like to create a new array from this array by grouping rows with the same string. e.g.
2 => string '20' (length=2) with 20 => string '20' (length=2) and with 9 => string '20' (length=2)
and
5 => string '13' (length=2) with 5 => string '13' (length=2)
etc.
and order the new created array rows based on how many times the string occures there.
Order need to be DESC from the most occurrences to the last like a classic top something chart (The most present strings are first and the least are low)
So, the modified array will look like this:
array (size=21)
0 => string '20' (length=2)
1 => string '13' (length=2)
...
I also need somehow to handle null results e.g. 17 => null to be not incorporated at all in the final array modified result.
This should do the trick:
// Filter the "null results" first
$myarray = array_filter($myarray, create_function('$arg', '
return !is_null($arg);
'));
$occurrences = array_count_values($myarray);
// EDIT: arsort preserves the key => value correlation
arsort($occurrences, SORT_NUMERIC);
var_dump(array_keys($occurrences));
Try this.
$result = array_count_values($a);
arsort($result);
$result = array_keys($result);
I try to found in my all PHP files the strings inside the i18n functions. Here is an example:
$string = '__("String 2"); __("String 3", __("String 4"));' . "__('String 5'); __('String 6', __('String 7'));";
var_dump(preg_match_all('#__\((\'|")([^\'"]+)(\'|")\)#', $string, $match));
var_dump($match);
I wanna get this result:
array
0 => array
0 => string 'String 2' (length=8)
1 => string 'String 3' (length=8)
2 => string 'String 4' (length=8)
3 => string 'String 5' (length=8)
4 => string 'String 6' (length=8)
4 => string 'String 7' (length=8)
But unfortunately I get this result
array
0 => array
0 => string '__("esto es una prueba")' (length=24)
1 => string '__("esto es una prueba 2")' (length=26)
2 => string '__("prueba 4")' (length=14)
3 => string '__('caca')' (length=10)
4 => string '__('asdsnasdad')' (length=16)
1 => array
0 => string '"' (length=1)
1 => string '"' (length=1)
2 => string '"' (length=1)
3 => string ''' (length=1)
4 => string ''' (length=1)
2 => array
0 => string 'esto es una prueba' (length=18)
1 => string 'esto es una prueba 2' (length=20)
2 => string 'prueba 4' (length=8)
3 => string 'caca' (length=4)
4 => string 'asdsnasdad' (length=10)
3 => array
0 => string '"' (length=1)
1 => string '"' (length=1)
2 => string '"' (length=1)
3 => string ''' (length=1)
4 => string ''' (length=1)
Thanks in advance.
preg_match_all('/(?<=\(["\']).*?(?=[\'"])/', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];
Simple.
Note that I am using ( as an entry point to the match. If you have more exotic input you should provide it.
Don't capture the quotes.
preg_match_all('#__\([\'"]([^\'"]+)[\'"]\)#', $string, $match);
Also take a look at the flags parameter for preg_match_all() for different output formats.
I need some help with tables.
I've got some example data:
array
0 =>
array
'ID' => string '1' (length=1)
0 => string '1' (length=1)
'Name' => string 'Jon' (length=3)
1 => string 'Jon' (length=3)
'SecondName' => string 'Dee' (length=3)
2 => string 'Dee' (length=3)
'Date' => string '3, 2011' (length=7)
3 => string '3, 2011' (length=7)
'TotalHours' => string '06:48' (length=5)
4 => string '06:48' (length=5)
'TotalFee' => string '124' (length=3)
5 => string '124' (length=3)
1 =>
array
'ID' => string '3' (length=1)
0 => string '3' (length=1)
'Name' => string 'Some ' (length=5)
1 => string 'Some ' (length=5)
'SecondName' => string 'Preson' (length=6)
2 => string 'Preson' (length=6)
'Date' => string '3, 2011' (length=7)
3 => string '3, 2011' (length=7)
'TotalHours' => string '32:06' (length=5)
4 => string '32:06' (length=5)
'TotalFee' => string '436' (length=3)
5 => string '436' (length=3)
2 =>
array
'ID' => string '3' (length=1)
0 => string '3' (length=1)
'Name' => string 'Some ' (length=5)
1 => string 'Some ' (length=5)
'SecondName' => string 'Preson' (length=6)
2 => string 'Preson' (length=6)
'Date' => string '4, 2011' (length=7)
3 => string '4, 2011' (length=7)
'TotalHours' => string '10:00' (length=5)
4 => string '10:00' (length=5)
'TotalFee' => string '1345' (length=4)
5 => string '1345' (length=4)
Now I'm trying to dynamically create html table with this data grouped by person, so Name/Second name wont repeat in every row. Is it possible to merge Name cells?
/-----------------------------------------\
| | month 1 | hours | cash |
| Person 1 |------------------------------|
| | month 2 | hours | cash |
|-----------------------------------------|
| Person 2 | month 1 | hours | cash |
\-----------------------------------------/
etc...
Is it possible?
The thing you are looking for is a rowspan.
Check out this link: http://www.htmlcodetutorial.com/tables/index_famsupp_30.html
Usage:
<TABLE BORDER=2 CELLPADDING=4>
<TR>
<TH ROWSPAN=3 BGCOLOR="#99CCFF">Production</TH>
<TD>Raha Mutisya</TD> <TD>1493</TD>
</TR>
<TR>
<TD>Shalom Buraka</TD> <TD>3829</TD>
</TR>
<TR>
<TD>Brandy Davis</TD> <TD>0283</TD>
</TR>
<TR>
<TH ROWSPAN=3 BGCOLOR="#99CCFF">Sales</TH>
<TD>Claire Horne</TD> <TD>4827</TD>
</TR>
<TR>
<TD>Bruce Eckel</TD> <TD>7246</TD>
</TR>
<TR>
<TD>Danny Zeman</TD> <TD>5689</TD>
</TR>
</TABLE>
</TABLE></html>
You need to add this properly ofcourse, but it gives you the basic idea.
Also you need to order the results by the person's name (or ID) and keep track if it changes when running through the resultset. That's the easiest solution.