Thursday, 15 January 2015

html - SQL group names as table header rows using PHP -


I am trying to produce a dynamic table using PHP which looks something like this:

  Paul 56 Paul 6 Paul 78 Paul 34 Paul 76 Mark 56 Mark 5 Mark 34 Mark 87 Mark 23 Mark 765 Basically what I want to produce is something like this:   pre & lt; & Lt; Tr colspan = "2" & gt; Name: Paul & lt; / Tr & gt; & Lt; TR & gt; & Lt; TD & gt; 56 & lt; / TD & gt; & Lt; TD & gt; Edit & lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; 6 & lt; / TD & gt; & Lt; TD & gt; Edit & lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; 78 & lt; / TD & gt; & Lt; TD & gt; Edit & lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; 34 & lt; / TD & gt; & Lt; TD & gt; Edit & lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; 76 & gt; & Lt; TD & gt; Edit & lt; / TD & gt; & Lt; / TR & gt; & Lt; Tr colspan = "2" & gt; Name: Mark & ​​lt; / Tr & gt; & Lt; TR & gt; & Lt; TD & gt; 56 & lt; / TD & gt; & Lt; TD & gt; Edit & lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; 5 & ​​lt; / TD & gt; & Lt; TD & gt; Edit & lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; 34 & lt; / TD & gt; & Lt; TD & gt; Edit & lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; 87 & lt; / TD & gt; & Lt; TD & gt; Edit & lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; 23 / TD & gt; & Lt; TD & gt; Edit & lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; 765 / td> & Lt; TD & gt; Edit & lt; / TD & gt; & Lt; / TR & gt; & Lt; / Table & gt; 

How my PHP code looks so far:

  & lt ;? Php $ name_holder = null; ($ I = 0; $ i & lt; Count ($ data); $ i ++) {If ($ name_holder! = $ Data [$ i] - & gt; name) {$ name_holder = $ data [$ i ] - & gt; Name; // sure what to do here ?? } Resonates "& lt; tr & gt;"; Echo "& lt; td align = 'left' & gt; $ Data [$ i] - & gt; Name. "& Lt; / td>"; Echo "& lt; td align = 'left' & gt; $ Data [$ i] - & gt; Bell. "& Lt; / td>"; Echo "& lt; / tr & gt;"; }? & Gt; Select  

sql

 , by name name by name checkin_tbl by name, name by ASC;  

You do not need to group your data at the database level. It is possible to do this, but to read related data for each group, you need another SELECT , and as a result, a large number of databases are being sent to the database.

I have chosen to read data using a direct query, which would be something like this:

  SELECT name, by hour checkin_tbl ORDER by name ASC; I have not supplied the database code below, because it is not clear what database you are using (like MySQL) or what interface you are using (eg. .pdo). I have followed the data source using an array like this - I'm sure you swap it with  fetchAll ()  or  foreach ()  and one line .  

In this way, try to do something like this:

  & lt ;? Php // data from your database $ rawData = [['Paul', 56], ['Paul', 6], ['Mark', 56], ['Mark', 5],]; // Let's reprint this $ = []; Foreign currency ($ pair as $ rawData) {$ name = $ pair [0]; If (! Isset ($ $ [$ name])) {$ out [$ name] = []; } // Pop up the value in the list $ $ [$ name] [] = $ pair [1]; } // Use this to see your structure #print_r ($ out); ? & Gt; & Lt ;! - This is your rendering section - & gt; & Lt ;? Php foreach ($ $ = $ data as $ & gt; $ data):? & Gt; & Lt; Tr colspan = "2" & gt; & Lt; Td> Name: & lt ;? Php echo htmlentities ($ name)? & Gt; & Lt; / Td> & Lt; / Tr & gt; & Lt ;? Php foreach ($ item as $ item):? & Gt; & Lt; Tr & gt; & Lt; Td> & Lt ;? Php echo hougements ($ item)? & Gt; & Lt; / Td> & Lt; Td> Edit & lt; / Td> & Lt; / Tr & gt; & Lt ;? Php endforeach? & Gt; & Lt ;? Php endforeach? & Gt;  

My strategy is to format as much data as possible outside of the template - your code and another answer (at the time of writing) here in my opinion is a mix of logic and layout Is doing it, and you need to separate them.

This is the reason why I have switched to open / stop PHP tags in the rendering section: This is normally HTML with PHP Snippet where dynamic output is required. If you do this, you will benefit from tag matching and syntax color in your IDE.

I've added htmlentities () to avoid XSS.


No comments:

Post a Comment