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>
Related
I have two HTML tables. Each has three rows and one column only. I'd like to join them programmatically such that I get one table with two columns and three rows.
Is there some function or hack to achieve this? How can I achieve this?
For example
First Table:
<table id="table_one">
<tbody>
<tr>
<td>
<label>data one</label>
</td>
</tr>
<tr>
<td>
<label>data two</label>
</td>
</tr>
<tr>
<td>
<label>data three</label>
</td>
</tr>
</tbody>
</table>
Second Table:
<table id="table_two">
<tbody>
<tr>
<td>
<label>data one</label>
</td>
</tr>
<tr>
<td>
<label>data two</label>
</td>
</tr>
<tr>
<td>
<label>data three</label>
</td>
</tr>
</tbody>
</table>
This is what I want from the above two tables:
<table id="table_three">
<tbody>
<tr>
<td>
<label>data one</label>
</td>
<td>
<label>data one</label>
</td>
</tr>
<tr>
<td>
<label>data two</label>
</td>
<td>
<label>data two</label>
</td>
</tr>
<tr>
<td>
<label>data three</label>
</td>
<td>
<label>data three</label>
</td>
</tr>
</tbody>
</table>
Well, this is a way to proceed (check in-code comments):
$table1 = <<<'_DATA_'
<table id="table_one">
<tbody>
<tr>
<td>
<label>data one</label>
</td>
</tr>
<tr>
<td>
<label>data two</label>
</td>
</tr>
<tr>
<td>
<label>data three</label>
</td>
</tr>
</tbody>
</table>
_DATA_;
$table2 = <<<'_DATA_'
<table id="table_two">
<tbody>
<tr>
<td>
<label>data one</label>
</td>
</tr>
<tr>
<td>
<label>data two</label>
</td>
</tr>
<tr>
<td>
<label>data three</label>
</td>
</tr>
</tbody>
</table>
_DATA_;
// Load teh 1st table
// Create a DOM object
$html1 = new simple_html_dom();
// Load HTML from a string
$html1->load($table1);
// Load teh 2nd table
// Create a DOM object
$html2 = new simple_html_dom();
// Load HTML from a string
$html2->load($table2);
// Find all rows from both tables
$rows1 = $html1->find('tr');
$rows2 = $html2->find('tr');
// Build teh 3rd table
$table3 = '<table id="table_three">
<tbody>';
// Insert rows cells from both initial tables
for ($i=0; $i < count($rows1); $i++) {
$table3 .= '<tr>';
// get row's innerhtml
$table3 .= $rows1[$i]->innertext;
$table3 .= $rows2[$i]->innertext;
$table3 .= '</tr>';
};
// finish the table
$table3 .= '</tbody>
</table>';
// Clear DOM objects
$html1->clear();
unset($html1);
$html2->clear();
unset($html2);
It creates this:
<table id="table_three">
<tbody>
</tbody>
</table>
<script type="text/javascript">
jQuery(document).ready(function($){
$('#table_one tr').each(function(index){
var row = $(this);
var row2 = $('#table_two tr').get(index);
$(row).append($(row2).find('td'));
$('#table_three').append($(row));
});
});
</script>
Here's how I'd do it:
// assume we have the tables in three strings:
// table_one in $str_1, table_two in $str_2, and table_three in $str_3.
// create $str_3 by copying $str_1 and replacing the table ID
$str_3 = $str_1;
$str_3 = str_replace('table_one', 'table_three', $str_3);
// load table_two and table_three in simple-html-dom and collect the TD elements
$tab_2 = str_get_html($str_2);
$tab_3 = str_get_html($str_3);
$tds_3 = $tab_3->find("td");
$tds_2 = $tab_2->find("td");
// go through the TDs in table_three and append the corresponding table_two TD
$acc = 0;
foreach ($tds_3 as &$td) {
$td->outertext = $td->outertext . $tds_2[$acc]->outertext;
$acc++;
}
// done!
echo $tab_3;
Not sure I understood the question, but:
<?php
// first and second table
//Select data
$reponse = $bdd->prepare('SELECT data1, data2 FROM '.RESEARCH.' WHERE X=X');
$reponse->execute(array(X));
?>
//Begin table
<table id="table_one_or_two">
<tbody>
//Loop the rows
while ($data = $reponse->fetch())
{
<tr>
<td>
<label>$data['data1']</label>
</td>
</tr>
}
//end of loop, end of table
</tbody>
</table>
// 3TH table
//Select data
$reponse = $bdd->prepare('SELECT data1, data2 FROM '.RESEARCH.' WHERE X=X');
$reponse->execute(array(X));
?>
//Begin table
<table id="table_three">
<tbody>
//Loop the rows
while ($data = $reponse->fetch())
{
<tr>
<td>
<label>$data['data1']</label>
</td>
<td>
<label>$data['data2']</label>
</td>
</tr>
}
//end of loop, end of table
</tbody>
</table>
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
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 }
?>
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>
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>