why does it loop more then once...am i missing something - php

There is only one record in the table so why does it loop like i have 5 table with one letter each
$query = "Select * from click_tracker";
$result = mysql_query($query);
$all_clicks = mysql_fetch_array($result);
foreach($all_clicks as $click){
print "
<table border=\"1\">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>
<tr>
<td>{$click['url_destination']}</td>
<td>{$click['count']}</td>
</tr>
</table>";
}
here is the table returned
<table border="1">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>
<tr>
<td>2</td>
<td>2</td>
</tr>
</table>
<table border="1">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>
<tr>
<td>2</td>
<td>2</td>
</tr>
</table>
<table border="1">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>
<tr>
<td>h</td>
<td>h</td>
</tr>
</table>
<table border="1">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>
<tr>
<td>h</td>
<td>h</td>
</tr>
</table>
<table border="1">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>
<tr>
<td>5</td>
<td>5</td>
</tr>
</table>
<table border="1">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>
<tr>
<td>5</td>
<td>5</td>
</tr>
</table>

mysql_fetch_array fetches one row as an array. When you try to loop over that result with your foreach, you are actually looping through all the columns of the row you returned (twice, actually, because by default, mysql_fetch_array returns an array with both numeric and indexed keys!)
If you want to get all the rows in your result set (and you more than likely do), you need to use a while loop to keep fetching rows until there aren't anymore:
$all_clicks = array();
while ($row = mysql_fetch_array($result))
{
$all_clicks[] = $row;
}
and then when you iterate over $all_clicks, each iteration will have a complete row.

mysql_fetch_array() returns rows, it looks like your foreach is looping over fields in a row not rows in a result set.

You appear to be printing multiple tables. I don't think this is what you intend though. You need to print the table's opening and closing tags, and the headings, outside of the loop. You should also call mysql_fetch_array() in the loop and not just once.
print "
<table border=\"1\">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>";
$query = "Select * from click_tracker";
$result = mysql_query($query);
while ($click = mysql_fetch_array($result)) {
print "
<tr>
<td>{$click['url_destination']}</td>
<td>{$click['count']}</td>
</tr>";
}
print "</table>";
You should also consider escaping the data in $click, but I don't know what your data looks like so I'm not sure what to put in the area just between the while and print statements.

You need to do it like this:
$query = "Select * from click_tracker";
$result = mysql_query($query);
while($click = mysql_fetch_assoc($result)) {

do a print_r($all_clicks) and check the result is what you expect it to be.
you don't really need to use a foreach if there's only one result.
$query = "Select * from click_tracker";
$result = mysql_query($query);
$all_clicks = mysql_fetch_array($result);
print "
<table border=\"1\">
<tr>
<th>Location</th>
<th>Visit Count</th>
</tr>
<tr>
<td>{$all_clicks['url_destination']}</td>
<td>{$all_clicks['count']}</td>
</tr>
</table>";

Related

Generate new table after 10 iteration of tr tags [duplicate]

This question already has an answer here:
How can I get two items in a foreach loop in PHP? [duplicate]
(1 answer)
Closed 2 years ago.
I have a table with looping tr tags I am looking to break after every second tr tag.
like
My table looks like this after the for loop.
<table class="table">
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
<tr>
<td>10</td>
<td>11</td>
<td>12</td>
after modulo logic, I want to show'em like this
<table class="table">
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</tbody>
</table>
<table class="table">
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
<tr>
<td>10</td>
<td>11</td>
<td>12</td>
</tr>
</tbody>
</table>
And here is my PHP script so far.
I have tried all sort of arrangement b
ut unable to achieve those layout
<?php $num = 1; ?>
<table class="Table">
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<?php
for ( $x = 1; $x <= 12; $x++ ) {
if($num%2 == 0) {
?>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<?php
}
?>
<?php
if($num %2 == 1) {
?>
<table class="Table">
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<?php
}
$num++;
}
?>
</tbody>
</table>
I know I am doing a silly mistake but can't figure it out.
I appreciate your help.
Create a function that returns you a table. For every dataset of size 6, call the function and get your table. You can use array_chunk to chunk the datasets into smaller chunks and use heredoc syntax for better readability.
getTable() function:
<?php
function getTable($data){
$table = <<<EOD
<table class="table">
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
EOD;
$rows = "";
foreach(array_chunk($data,3) as $values){
$rows .= "<tr>";
foreach($values as $value){
$rows .= "<td>" . $value . "</td>";
}
$rows .= "/<tr>";
}
$table .= $rows;
$table .= <<<EOD
</tbody>
</table>
EOD;
return $table;
}
Driver code:
<?php
$arr = [1,2,3,4,5,6,7,8,9,10,11,12];
foreach(array_chunk($arr,6) as $set_for_table){
echo getTable($set_for_table);
}
for dynamic tables you can iterate your entire table and set the two tr values.
using your example:
<?php $total = 100; ?>
<?php for ($i=0; $i < $total; $i+=6):?>
<table class="table">
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td><?=$i+1?></td>
<td><?=$i+2?></td>
<td><?=$i+3?></td>
</tr>
<tr>
<td><?=$i+4?></td>
<td><?=$i+5?></td>
<td><?=$i+6?></td>
</tr>
</tbody>
</table>
<?php endfor; ?>
will generate $total/6 tables each with two <tr>

How to show mysql multi row / mysql_fetch_array results in a table?

I am trying to show mysql_fetch_array() results in a table.
I want to show guests name,their country and their agreed time who are traveling on a same date.
Following code works fine. The code fetches the row values and prints it.
$select_guests = mysql_query('SELECT name FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_guests, MYSQL_ASSOC)) { //visitor / guest loop starts here
echo $row['name'].'<br/>';
}
$select_country = mysql_query('SELECT country FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_country, MYSQL_ASSOC)) { //country of visitor / guest loop starts here
echo $row['country'].'<br/>';
}
$select_agreed_time = mysql_query('SELECT agreed_time FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_agreed_time, MYSQL_ASSOC)) { //visitor / guest agreed time loop starts here
echo $row['agreed_time'].'<br/>';
}
if there are 5 guests for a same date I am getting all of their names one below another when I execute the above code. Same I am getting there countries and agreed time too.
Now I want to show those results in a HTML table.
I tried several line of code but nothing works.
My HTML table should be as following:
<table class="table-fill">
<thead>
<tr>
<th class="text-left">Name</th>
<th class="text-left">From</th>
<th class="text-left">Agreed Time</th>
</tr>
</thead>
<tbody class="table-hover">
<tr>
<td class="text-left">Name 1</td>
<td class="text-left">Country 1</td>
<td class="text-left">Ag Time 1</td>
</tr>
<tr>
<td class="text-left">Name 2</td>
<td class="text-left">Country 2</td>
<td class="text-left">Ag Time 2</td>
</tr>
<tr>
<td class="text-left">Name 3</td>
<td class="text-left">Country 3</td>
<td class="text-left">Ag Time 3</td>
</tr>
<tr>
<td class="text-left">Name 4</td>
<td class="text-left">Country 4</td>
<td class="text-left">Ag Time 4</td>
</tr>
<tr>
<td class="text-left">Name 5</td>
<td class="text-left">Country 5</td>
<td class="text-left">Ag Time 5</td>
</tr>
</tbody>
</table>
How can create that table td s according to my mysql_fetch_array() ?
The above table structure is for 5 guests found or resulted by mysql_fetch_array()
First of all I think you dont need 3 different queries for your solution..
<table class="table-fill">
<thead>
<tr>
<th class="text-left">Name</th>
<th class="text-left">From</th>
<th class="text-left">Agreed Time</th>
</tr>
</thead>
<?php
$result = mysql_query('SELECT name,country,agreed_time FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<tr>
<td>
<?php echo $row['name']; ?>
</td>
<td>
<?php echo $row['country'];?>
</td>
<td>
<?php echo $row['agreed_time']; ?>
</td>
</tr>
<?php
}
?>
</table>
Firstly, you should use mysqli.
Secondly, shouldn't be sending so many queries to the database for information you can get in one query;
$select_guests = $mysqli->query('SELECT * FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error());
Next, you want to fetch the number of rows.
$rows = $mysqli
You should also look into the PHP for function;
for ($i = 1; $i < $rows; $i++) {
$thisRow = $select_guests->fetch_row()
echo
' <tr>
<td class="text-left">'.$select_guests['name'].'</td>
<td class="text-left">'.$select_guests['country'].'</td>
<td class="text-left">'.$select_guests['time'].'</td>
</tr>
'; //This last line is to insert a line break and indent (for tidy HTML)
}
Give this a go, hopefully I've helped you.
I haven't completely solved it for you though, in order to change over to mysqli, you will need to make a few small changes which you can find in the mysqli link I sent you. The benefits are worth it.
$select_all = mysql_query('SELECT * FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting all details for the same date
while($row = mysql_fetch_array($select_all , MYSQL_ASSOC)) {//loop starts here
<tr>
<td>
<?php echo $row['name']; ?>
</td>
<td>
<?php echo $row['country'];?>
</td>
<td>
<?php echo $row['agreed_time']; ?>
</td>
</tr>
}

foreach loop not generating the right html table

I'm trying to get this output from returned data fetched from mysql:
<table>
<thead>
<tr>
<th></th><th><img src='img1.jpg'></th><th><img src='img2.jpg'></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan='5'>Features</td>
<td>LCD</td><td>Yes</td><td>No</td>
<td>Auto Pilot</td><td>No</td><td>No</td>
<td>Fast Generation</td><td>Yes</td><td>No</td>
<td>Dual Cores</td><td>No</td><td>Yes</td>
</tr>
</tbody>
</table>
But I have trouble getting the following code to achieve that output with one foreach loop. It uses in_array to check whether each value from $featured_tests exists in the returned data.
$featured_tests = array("LCD","Auto Pilot","Fast Generation","Dual Cores");
$table_head ="<table><thead><tr><th></th>";
$table_colspan1 = "</tr></thead><tbody><tr><td colspan='5'>Features</td></tr>";
$table_end ="</tr></tbody></table>";
foreach($rows as $row)
{
$special_features = explode(",",$row->special_features);
$header_image .= "<th><img src='".$row->image_url."'></th>";
foreach($featured_tests as $featured_test)
{
$featured .= "<tr><td>".$featured_test."</td>";
if(in_array($featured_test,$special_features))
{
$featured .= "<td>Yes</td>";
}
else
{
$featured .= "<td>No</td>";
}
}
}
$table_html = $table_head.$header_image.$table_colspan1.$featured.$table_end;
But the result I'm getting is a mess. Each value in $featured_tests is iterating over and over again for each product and thus results in a very long table. Can anyone help me correct my code to get the ideal output?
Here's the result:
<table>
<thead>
<tr>
<th></th><th><img src='img1.jpg'></th><th><img src='img2.jpg'></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan='5'>Features</td>
</tr>
<tr>
<td>LCD</td><td>Yes</td>
</tr>
<tr>
<td>Auto Pilot</td>No<td></td>
</tr>
<tr>
<td>Fast Generation</td><td>Yes</td>
</tr>
<tr>
<td>Dual Cores</td>No<td>
</tr>
<tr>
<td>LCD</td><td>No</td>
</tr>
<tr>
<td>Auto Pilot</td>No<td></td>
</tr>
<tr>
<td>Fast Generation</td><td>No</td>
</tr>
<tr>
<td>Dual Cores</td>Yes<td>
</tr>
</tbody>
</table>
you are creating new rows <tr> inside on most deep foreach...
take this example to make what you want:
<table>
<thead>
<th>Col 1</th><th>Col 2</th>
</thead>
<tbody>
<?php foreach($large_array as $less_array): ?>
<tr>
<?php foreach($less_array as $row): ?>
<!-- <td> contents etc</td>-->
<?php endforeach?>
</tr>
<?php endforeach;?>
</tbody>
</table>

Fetch Values of same name item, without repeating item name?

I am making a page of my products, in which i have many products with having same names but different specifications and prices.
PHP code:
<code>
$SelectList = "SELECT a.p_id, a.dis,a.name,b.p_id,b.rate
FROM tst1 a
INNER JOIN max b
ON a.p_id = b.p_id GROUP BY name";
$Query = mysql_query($SelectList) or die(mysql_error());
$Num_rows = mysql_num_rows($Query);
$array = array();
while($Data=mysql_fetch_assoc($Query))
{
$array['name'][] = $Data['name'];
$array['rate'][] = $Data['rate'];
$array['dis'][] = $Data['dis'];
}
</code>
<code>
<table border="1">
<thead>
<th>Description</th>
<th>Unit</th>
<th>Qty</th>
<th>Rate</th>
</thead>
<tr>
<td colspan="0">BUTTERFLY VALVES</td>
</tr>
<tr>
<td>65 mm</td>
<td>Nos</td>
<td>15</td>
<td>40%</td>
</tr>
<tr>
<td colspan="0">BUTTERFLY VALVES</td>
</tr>
<tr>
<td>85 mm</td>
<td>Nos</td>
<td>45</td>
<td>40%</td>
</tr>
<tr>
<td colspan="0">WASHER VALVES</td>
</tr>
<tr>
<td>32 mm</td>
<td>Nos</td>
<td>95</td>
<td>30%</td>
</tr>
</table>
</code>
So what i want is, if product name is same so just fetch only "name" once, and its "unit, qty & rates" all of them.
<code>
<table style="position:relative; top:10px; bottom:10px; left:200px" border="1">
<thead>
<th>Description</th>
<th>Unit</th>
<th>Qty</th>
<th>Rate</th>
</thead>
<tr>
<td colspan="0">BUTTERFLY VALVES</td>
</tr>
<tr>
<td>65 mm</td>
<td>Nos</td>
<td>15</td>
<td>40%</td>
<td>85 mm</td>
<td>Nos</td>
<td>45</td>
<td>40%</td>
</tr>
<tr>
<td colspan="0">WASHER VALVES</td>
</tr>
<tr>
<td>32 mm</td>
<td>Nos</td>
<td>95</td>
<td>30%</td>
</tr>
</table>
</code>
check this subquery.. this will help you out..
$qry = mysqli_query("select * from product group by name");
while($row = mysqli_fetch_array($qry))
{
echo "<tr><td colspan="0">'".$row['name']."'</td></tr>";
$qry2 = mysqli_query("select * from product where name = '".$row['name']."'");
echo "<tr>";
while($row2 = mysqli_fetch_array($qry2))
{
echo "<td>'".$row2['mm']."'</td><td>'".$row2['nos']."'</td><td>'".$row2['qty']."'</td><td>'".$row2['percentage']."'</td>";
}
echo "</tr>";
}
You may use either distinct or group by operator in your SQL to do so.
Usage of distinct looks like this:
SELECT DISTINCT column_name,column_name
FROM table_name;
You may take a look at this example of distinct.
Usage of group by looks like this:
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
Here is an example of group by.
I hope this helps.

How can I add a link to a table in Smarty?

I have a requirement to output a table using Smarty and then have the user select a row to take them to another page.
I can display the table fine:
{html_table cols="Job Title,Salary,Sector,Location" loop=$results}
which gives:
<table border="1">
<thead>
<tr>
<th>Job Title</th>
<th>Salary</th>
<th>Sector</th>
<th>Location</th>
</tr>
</thead>
<tbody>
<tr>
<td>Dog walker</td>
<td>20000</td>
<td>None</td>
<td>London</td>
</tr>
<tr>
<td>F1 Driver</td>
<td>10000000</td>
<td>Financial Services</td>
<td>Scotland</td>
</tr>
</tbody>
</table>
but I am not sure if it is possible to add a hyperlink as an additional column to the table that links to a page using a hidden id.
So I would want something link this:
<table border="1">
<thead>
<tr>
<th>Job Title</th>
<th>Salary</th>
<th>Sector</th>
<th>Location</th>
<th>Apply</th>
</tr>
</thead>
<tbody>
<tr>
<td>Dog walker</td>
<td>20000</td>
<td>None</td>
<td>London</td>
<td>Apply</td>
</tr>
<tr>
<td>F1 Driver</td>
<td>10000000</td>
<td>Financial Services</td>
<td>Scotland</td>
<td>Apply</td>
</tr>
</tbody>
</table>
Is that possible?
Yes, but you need to modify the $results array before you pass it to Smarty so that the 4th element of each row contains the link as a string. There's no way to have {html_table} generate the link for you.

Categories