Arranging array results from a MySQL query - php

I'm making a brochure generator based on inventory in a database. Due to the way the on-paper brochure looks, the order of items display will not match the way mysql_fetch_array works. Each user chosen category holds four items in a 2X2 grid.
Category 1 ---------Category 2
[1] [2] ---------[5] [6]
[3] [4] ---------[7] [8]
Category 3 ---------Category 4
[9] [10] ---------[13] [14]
[11] [12] ---------[15] [16]
I've already created a basic table as a placeholder for the items in this pattern. This is my query to retrive the items.
$query = "SELECT t1.*, image_path FROM flyer_item AS t1
LEFT JOIN product_images AS t2 ON t1.product_id = t2.product_id WHERE id_page = '".$id_page."'";
echo $query."<br>";
$result = mysql_query($query);
echo '<h3 class = "splitter">Items</h3>';
//-create while loop and loop through result set
//Due to the unique item arrangement patter, we fill
//an array with the fetched array results.
$iArray = array();
while($row=mysql_fetch_array($result))
{
$iArray[] = $row;
$square = $row['square'];
$item_name = $row['item_name'];
$sales_info = $row['sales_info'];
$link = $row['image_path'];
$sku_item_number = $row['sku_item_number'];
if (empty($link))
{
$link = '../imagen/no_imagen.gif';
}
}
One page can hold up to a maximum of 24 items, all following the pattern mentioned above to make sure items are in their respective categories. I'm contemplating making another database table called Categories that will store its four items, but is there a way I can use the second array, iArray, to store the number indicating where the items should go?
The actual table looks like this, to match the original document.
<table width="100%" border="1" bordercolordark="#000000" bordercolorlight="#000000">
<tr>
<td> </td>
<td colspan="2" align="center"><?php echo $category1; ?></td>
<td> </td>
<td colspan="2" align="center"><?php echo $category2; ?></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>Item 1</td>
<td>Item 2</td>
<td> </td>
<td>Item 5</td>
<td>Item 6</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>Item 3</td>
<td>Item 4</td>
<td> </td>
<td>Item 7</td>
<td>Item 8</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td colspan="2" align="center"><?php echo $category3; ?></td>
<td> </td>
<td colspan="2" align="center"><?php echo $category4; ?></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>Item 9</td>
<td>Item 10</td>
<td> </td>
<td>Item 13</td>
<td>Item 14</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>Item 11</td>
<td>Item 12</td>
<td> </td>
<td>Item 15</td>
<td>Item 16</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td colspan="2" align="center"><?php echo $category5; ?></td>
<td> </td>
<td colspan="2" align="center"><?php echo $category6; ?></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>Item 17</td>
<td>Item 18</td>
<td> </td>
<td>Item 21</td>
<td>Item 22</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>Item 19</td>
<td>Item 20</td>
<td> </td>
<td>Item 23</td>
<td>Item 24</td>
<td> </td>
</tr>
</table>

Related

Sql query from database

I need some help with how I can soft query from sql.
I have a php script with an html page who is showing the result from the query.
I have this query:
$ready_orders = DB::query( 'SELECT * FROM ordertable where orderid is NOT null ORDER by id DESC')->fetchAll( DB::FETCH_ASSOC );
From the HTML I have this;
<tal:block tal:condition="exists:orders">
<table>
<tr>
<td>amount</td>
<td> want counter here</td>
<td>result 1</td>
<td>result 2</td>
<td>result 3</td>
<td>result 4</td>
</tr>
<tr tal:repeat="order orders">
<td tal:content="">
....
</td>
<td tal:content="order/id">
....
</td>
<td tal:content="order/orderid">
....
</td>
<td tal:content="order/productid">
....
</td>
<td tal:content="order/processed">
....
</td>
<td>
</td>
</tr>
</table>
The query is doing all I want, but need the result to be in a listing.
Like a counter 1,2,3,4,5 etc
Is it possible?
include a
select count * from ordertable
Then insert this result in the first
SELECT COUNT(orderid) AS Order_Count
FROM ordertable
WHERE orderid IS NOT NULL
using foreach loop
<tal:block tal:condition="exists:orders">
<table>
<tr>
<td>amount</td>
<?php
$i = 1;
foreach($ready_orders as $result){ ?>
<td>result <?php echo $i++; ?></td> //your counter
<?php } ?>
</tr>
<tr tal:repeat="order orders">
<td tal:content="">
....
</td>
<td tal:content="order/id">
....
</td>
<td tal:content="order/orderid">
....
</td>
<td tal:content="order/productid">
....
</td>
<td tal:content="order/processed">
....
</td>
<td>
</td>
</tr>

Getting tags in DOMDocument

I'm trying to get the HTML markup of a table in a page:
$new_dom = new DOMDocument();
$table = '';
$nodesTable = $this->dom->getElementsbyTagName("table");
foreach($nodesTable as $nodeTable){
$color = $nodeTable->getAttribute('bordercolordark');
if ($color == '#73BAFF') {
$table = $nodeTable;
}
}
$new_dom->appendChild($table);
echo $new_dom->saveHTML();
Here is somepage.html:
<html>
<table>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
</table>
<table border="1" cellpadding="0" width="500" bordercolorlight="#ACD6FF" bordercolordark="#73BAFF" align="center">
<tr>
<td rowspan="2" colspan="2" bgcolor="#73BAFF"> </td>
<td colspan="3" align="center" bgcolor="#ACD6FF"> Element 1 </td>
<td colspan="3" align="center" bgcolor="#ACD6FF"> Element 2 </td>
</tr>
<tr>
<td width="50" align="center" bgcolor="#ACD6FF"> 50 </td>
<td width="50" align="center" bgcolor="#ACD6FF"> 50 </td>
<td width="50" align="center" bgcolor="#ACD6FF"> 50 </td>
<td width="50" align="center" bgcolor="#ACD6FF"> 50 </td>
<td width="50" align="center" bgcolor="#ACD6FF"> 50 </td>
<td width="50" align="center" bgcolor="#ACD6FF"> 50 </td>
</tr>
<tr>
<td bgcolor="#ACD6FF" width="155" align="center"> Row 1</td>
<td bgcolor="#ACD6FF" width="45" align="center"> 30 </td>
<td align="center"> 50 </td>
<td align="center"> 50 </td>
<td align="center"> 50 </td>
<td align="center"> 50 </td>
<td align="center"> 50 </td>
<td align="center"> 50 </td>
</tr>
<tr>
<td bgcolor="#ACD6FF" width="155" align="center"> Row 2</td>
<td bgcolor="#ACD6FF" width="45" align="center"> 30 </td>
<td align="center"> 60 </td>
<td align="center"> 60 </td>
<td align="center"> 60 </td>
<td align="center"> 60 </td>
<td align="center"> 60 </td>
<td align="center"> 60 </td>
</tr>
<tr>
<td bgcolor="#ACD6FF" width="155" align="center"> Row 3</td>
<td bgcolor="#ACD6FF" width="45" align="center"> 30 </td>
<td align="center"> 70 </td>
<td align="center"> 70 </td>
<td align="center"> 70 </td>
<td align="center"> 70 </td>
<td align="center"> 70 </td>
<td align="center"> 70 </td>
</tr>
</table>
<table>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
</table>
<table>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
</table>
</html>
$new_dom just outputs \n instead of HTML markup. I tried looking at this answer: PHP DOMDocument stripping HTML tags, but appending the table this way didn't work either.
Fatal error: Uncaught exception 'DOMException' with message 'Wrong Document Error'
So you cannot move nodes from one document to another... If you want to do that, you have to use importNode() with the deep flag.
$dom = new DOMDocument();
$dom->loadHTMLFile('x.html');
$new_dom = new DOMDocument();
$table = '';
$nodesTable = $dom->getElementsbyTagName("table");
foreach($nodesTable as $nodeTable){
$color = $nodeTable->getAttribute('bordercolordark');
if ($color == '#73BAFF') {
$table = $new_dom->importNode($nodeTable, true);
}
}
$new_dom->appendChild($table);
echo $new_dom->saveHTML();
This imports only the table element, but not the children...
note: I'd disable the entity loader in your case libxml_disable_entity_loader(true);. I am not sure whether XEE attacks work with loadHTML() too, but just for the sake of security.

How to automatically generate rowspan using php

In this code everything is perfect except that I could not generate rowspan in the Internal Grade cell. this line <td rowspan="<?php echo $cols;?>"> hides the bottom border while I expect to span the rows every time the code generates rows. Thanks for your input and sorry for using mysql() function. I am newbie.
<?php $cols=mysql_num_rows($grds); ?>
<table>
<?php do{ ?>
<tr>
<td> </td>
//This hide the bottom border
<td rowspan="<?php echo $cols;?>">Internal<br />Grades</td>
<td colspan="2"> <?php echo strtoupper($row_grds['grade_name']); ?></td>
<td><?php echo strtoupper($row_grds['igrade']); ?></td>
<?php } while ($row_grds=mysql_fetch_assoc($grds)); ?>
</tr>
</table>
The output is like below:
output is here
The code generated by php is:
<table>
<tr>
<td> </td>
<td rowspan="1"> </td>// A
<td colspan="2"> RHYMES</td>
<td align="center">B</td>
</tr>
<tr>
<td> </td>
<td rowspan="2"> </td>//B
<td colspan="2"> CONVERSATION</td>
<td align="center">A</td>
</tr>
</table>
I want to merge A and B
Expected output is:
<table>
<tr>
<td> </td>
<td rowspan="2"> Internal<br /> Grade</td>
<td colspan="2"> RHYMES</td>
<td align="center">B</td>
</tr>
<tr>
<td> </td>
<td colspan="2"> CONVERSATION</td>
<td align="center">A</td>
</tr>
</table>
You can do something similar to this:
<table>
<?php $row_grds = mysql_fetch_assoc($grds); ?>
<tr>
<td> </td>
<td rowspan="<?=$cols;?>">Internal<br />Grades</td>
<td colspan="2"> <?=strtoupper($row_grds['grade_name']);?></td>
<td><?=strtoupper($row_grds['igrade']);?></td>
</tr>
<?php while ($row_grds=mysql_fetch_assoc($grds)): ?>
<tr>
<td> </td>
<td colspan="2"> <?=strtoupper($row_grds['grade_name']);?></td>
<td><?=strtoupper($row_grds['igrade']);?></td>
</tr>
<?php endwhile; ?>
Its not perfect but you are using mysql_fetch_assoc, which on its own is not perfect either :D

WordPress $wpdb->get_results show all the rows in a table

In WordPress, I have a table called wp_users
and I want to show all of them in a custom html table. So, my query goes like this
<table>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td> Email </td>
</tr>
<?php
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM wp_users");
foreach ( $rows as $row ) ?>
<tr>
<td>echo $row->first_name;</td>
<td>echo $row->first_name;</td>
<td> echo $row->first_name;</td>
</tr>
<?php
}
?>
This one is giving result for all the first_name inside the first table like this
<tr>
<td>Test 1Test 2</td>
<td>Test 12Test 2</td>
<td> test#test.comtest2#test.com</td>
</tr>
but I want the result be like this
<table>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td> Email </td>
</tr>
<tr>
<td>Test 1</td>
<td>Test 12</td>
<td> test#test.com</td>
</tr>
<tr>
<td>Test 2</td>
<td>Test 2</td>
<td> test2#test.com</td>
</tr>
</table>
So, Can someone tell me how to do this?
Any help and suggestions wll be really appreciable. Thanks
The answer is simple you just have to give the correct columnname inside foreach loop.
Right now you are just trying to print the firstname. And there is a bracket"{" missing after the foreach loop
<table>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td> Email </td>
</tr>
<?php
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM wp_users");
foreach ( $rows as $row )
{
?>
<tr>
<td>echo $row->first_name;</td>
<td>echo $row->Lastname;</td>
<td> echo $row->Email;</td>
</tr>
<?php }
?>

Display multiple database fields into a PHP table loop

I've been struggling with this for around 4 hours now...
What I'm trying to establish is pretty simple, I have a news table, I want to display the title of the news, the content, and a read more link, I know how to loop through a table and force , but this won't work in my case, the table should look like this in the end:
<table>
<tr>
<td>header one</td>
<td>header 2</td>
<td>header 3</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td>text one</td>
<td>text two</td>
<td>text 3</td>
</tr>
<tr>
<td>read more</td>
<td>read more </td>
<td>read more</td>
</tr>
</table>
What I have so far in my php is a code that will generate the rows and columns, but I want them to be distributed just like the sample table above in order not to mess the alignment of the text and the read more link ...
Here's my php code :
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<?php while ($record = mysql_fetch_assoc($result)): ?>
<?php $style++ ?>
<td width="33%" valign="top">
<h6><?php echo $record['title'] ?></h6>
<div class="service-sum"><?php echo $record['content'] ?></div>
<div class="findout">> find out more</div>
</td>
<?php if ($style == 3): ?>
</tr>
<?php $style = 0; ?>
<tr>
<td>
<div style="height:30px;"></div>
</td>
</tr>
<tr>
<?php endif ?>
<?php endwhile ?>
This one is working fine, but i'm displaying the title and the content and the read more link in one column, these should be distributed into 3 for design purposes...
Any help would be much appreciated, I looked all over the net and I can't find a solution for that!
try using this alignment. The design is almost same as yours.
<table>
<tr>
<td>
<table>
<tr>
<td>header one</td>
</tr>
<tr>
<td>text one</td>
</tr>
<tr>
<td>read more</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>header 2</td>
</tr>
<tr>
<td>text 2</td>
</tr>
<tr>
<td>read more</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>header 3</td>
</tr>
<tr>
<td>text 3</td>
</tr>
<tr>
<td>read more</td>
</tr>
</table>
</td>
</tr>
</table>
if there are only 3 records, the following should be fine. if there are more, some changes should be made. Please check your php code.
<table>
<tr>
<?php while ($record = mysql_fetch_assoc($result)) { ?>
<td>
<table>
<tr>
<td><?php echo $record['title'] ?></td>
</tr>
<tr><td height = "30px;"></td></tr>
<tr>
<td><?php echo $record['content'] ?></td>
</tr>
<tr>
<td>> find out more</td>
</tr>
</table>
</td>
<?php } ?>
</tr>
</table>

Categories