Generate table with merged cells - php

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.

Related

Multiple LEFT Join works correct in mysql but not in php

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.

Looping over Joined query

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!

What array function should I use for creating an index?

Hello guys I am trying to create an index of all words on html page that my crawler parses.
At this moment I have managed to breakdown the html page into an array of words and I have filtered out all the stop words.
At this stage I have a few problems.
The array of words from the parsed html page have words that are repeated, I like that because I still have to record how many times a word appeared in the page.
The array looks like this.
$wordsFromHTML =
array (size=119)
0 => string 'web' (length=3)
1 => string 'giants' (length=6)
2 => string 'vryheid' (length=7)
3 => string 'news' (length=4)
4 => string 'access' (length=6)
5 => string 'mails' (length=5)
6 => string 'mobile' (length=6)
7 => string 'february' (length=8)
8 => string 'access' (length=6)
9 => string 'mails' (length=5)
10 => string 'web' (length=3)
11 => string 'february' (length=8)
12 => string 'access' (length=6)
13 => string 'mails' (length=5)
14 => string 'desktop' (length=7)
15 => string 'february' (length=8)
16 => string 'hosting' (length=7)
17 => string 'web' (length=3)
18 => string 'giants' (length=6)
19 => string 'vryheid' (length=7)
20 => string 'february' (length=8)
22 => string 'us' (length=2)
Now I want to save all the words from the $wordsFromHTML to the $indesArray which is my final index.
It should look like this.
$indexArray = array('web'=>array('url'=>array(0,10,17)))
The problem is how to keep incrementing the position ($wordsFromHTML keys) for each word that was repeated from the $wordsFromHTML array in the final index array.
The index array should only have unique words and if another word that already exists try to come in, we use the already existing word which has the same URL and increment its position.
Hope you understand my question.

How to filter a dirty array into a clean stream of data

I have here what I would call a dirty array,
this dirty array needs to be filtered so it is a clean array e.g.
Below is the Array.
array
0 => string '1' (length=1)
1 => string 'FIRSTNAME A' (length=7)
2 => string 'LASTNAME B' (length=10)
3 => string '2011-12-08 16:15:37' (length=19)
4 => string '2' (length=1)
5 => string 'FIRSTNAME B' (length=7)
6 => string 'LASTNAME B' (length=10)
7 => string '2011-12-08 16:15:43' (length=19)
8 => string '3' (length=1)
9 => string 'FIRSTNAME C' (length=7)
10 => string 'LASTNAME C' (length=10)
11 => string '2011-12-08 16:15:48' (length=19)
12 => string '4' (length=1)
13 => string 'FIRSTNAME D' (length=7)
14 => string 'LASTNAME D' (length=10)
15 => string '2011-12-08 16:15:55' (length=19)
16 => string '6' (length=1)
17 => string 'FIRSTNAME E' (length=7)
18 => string 'LASTNAME E' (length=10)
19 => string '2011-12-08 16:16:08' (length=19)
I want the final output to look like
array[0]= 1, FIRSTNAME A, LASTNAME A, DATE
array[1]= 2, FIRSTNAME B, LASTNAME B, DATE
array[2]= 3, FIRSTNAME C, LASTNAME C, DATE
array[3]= 4, FIRSTNAME D, LASTNAME D, DATE
array[4]= 4, FIRSTNAME E, LASTNAME E, DATE
This should work
$clean = array_chunk($dirty, 4);
more about array_chunk
Wow, I'm going to take a stab at this. Not completely sure what your asking, but hopefully this will get us in some direction:
$cleanArray = array_chunk($dirtyArray,4);
foreach($cleanArray as $value) {
$finalArray[] = implode(", ",$value);
}
print_r($finalArray);

Get text that is within brackets with single or double quotes

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.

Categories