I have table with some data, and some other data I get with ajax.
I need to know how to display the response inside the table?
Code:
<?php $x = 1; foreach ($projects as $project){
echo "<tr class=\"parent\">";
echo "<td><i class=\"fa fa-chevron-down\"></td>";
echo "<td class=\"pid\">$project[pid]</td>";
echo "<td>$project[status]</td>";
echo "<td>$project[project_title]</td>";
echo "<td>$project[notes]</td>";
echo "<td>$project[responsible]</td>";
echo "<td>$project[start_date]</td>";
echo "<td>$project[completed_date]</td>";
echo "<td>$project[duration]</td>";
echo "<td>$project[completed]</td></tr>";
echo "<div class=\"cchild\" id=\"txtHint$x\">";
echo "</div>";
echo "</tbody>";
$x++;
}
The data should show inside the div element. As far as I know it's not allowed to put the div inside the table, but any other idea. How to get the data properly inside the table.
You first need to initialize a var with starting div and table tag and then in the for loop you can compute and append the tr td tags to the var. Once the loop is completed you can append the closing tags of div and table and the final output of the var will be table. Reference code:
$project = [1,2,3,4,5];
$div = "<div><table><thead></thead><tbody>";
foreach ($project as $p){
$div.="<tr><td>".$p."</td></tr>";
}
$div.="</tbody></table></div>";
You can add an empty tr with in table with specific class like
<?php $x = 1; foreach ($projects as $project){
echo "<tr class=\"parent\">";
echo "<td><i class=\"fa fa-chevron-down\"></td>";
echo "<td class=\"pid\">$project[pid]</td>";
echo "<td>$project[status]</td>";
echo "<td>$project[project_title]</td>";
echo "<td>$project[notes]</td>";
echo "<td>$project[responsible]</td>";
echo "<td>$project[start_date]</td>";
echo "<td>$project[completed_date]</td>";
echo "<td>$project[duration]</td>";
echo "<td>$project[completed]</td></tr>";
echo "<tr><td><div class="cchild"></div></td></tr>";
echo "</tbody>";
$x++;
}
Then replace the cchild div content with your ajax response.
Related
I need to convert JSON into a list using PHP, Tried code below but cannot make it work
$json=file_get_contents("http://feeds.mse.mk/service/FreeMSEFeeds.svc/ticker/JSON/8BA941D0-D6E6-44BD-8D8B-47FDB7A563FA");
$data = json_decode($json);
if (count($data->stand)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->stand as $idx => $stand) {
// Output a row
echo "<tr>";
echo "<td>$stand->AvgPrice</td>";
echo "<td>$stand->Description </td>";
echo "</tr>";
}
// Close the table
echo "</table>";
}
And I want to show list as here (not as a table):
http://prntscr.com/no1479
your all code is right but you can use stand class that is wrong your class is GetTickerJSONResult and so change the class stand to GetTickerJSONResult.
try this modified code..
<?PHP
$set =json_decode($json);
if (count($set->GetTickerJSONResult)) {
echo "<table>";
foreach ($set->GetTickerJSONResult as $idx => $stand) {
echo "<tr>";
echo "<td>$stand->AvgPrice</td>";
echo "<td>$stand->Description </td>";
echo "</tr>";
}
echo "</table>";
}
?>
It does not work because in $data->stand there is nothing.
My output is in the form of a 2d array. I have uploaded a sample file and the output is displayed like a paragraph. I want it to be displayed as a table.
Code:
<?php
require("reader.php"); // php excel reader
$file="sample.xls";
$connection=new Spreadsheet_Excel_Reader(); // our main object
$connection->read($file);
$startrow=1;
$endrow=1000;
for($i=$startrow;$i<$endrow;$i++){ // we read row to row
for($j=1;$j<=30;$j++) {
// so we get [2][3] and [3][3]
echo $connection->sheets[0]["cells"][$i][$j];
echo "\n";
}
echo "\n";
}
?>
Try this:
echo "<table>";
for($i=$startrow;$i<$endrow;$i++){ // we read row to row
echo "<tr>";
for($j=1;$j<=30;$j++) {
echo "<td>";
echo $connection->sheets[0]["cells"][$i][$j];
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
To begin with I know very little PHP and no java/javascript or jquery. I have created an html table populated from mysql database. It is for a call log. I am wanting to click on either a <td> or <tr> to open the corresponding record in a new page to be viewed in more detail. I have thought about putting the call_id value in a hidden column to be used in a variable somehow, but don't know where to go from there or if that is anywhere near the correct way to accomplish this.
Assuming that you're creating the table doing something like this,
$results = some_mysql_query;
foreach ($results as $index => $array) {
echo "<tr>";
echo "<td>";
echo $array['RecordNumber'];
echo "</td>";
echo "<td>";
echo $array['CallerName'];
echo "</td>";
echo "<td>";
echo $array['Time'];
echo "</td>";
echo "<td>";
echo $array['Duration'];
echo "</td>";
echo "</tr>";
}
Then you can add in a link by doing something like this:
$results = some_mysql_query;
foreach ($results as $index => $array) {
echo "<tr>";
echo "<td>";
echo "<a href='recordInfo.php?record='" . $array['RecordNumber'] . "'>" . $array['RecordNumber'] . "</a>";
echo "</td>";
echo "<td>";
echo "$array['CallerName'];
echo "</td>";
echo "<td>";
echo $array['Time'];
echo "</td>";
echo "<td>";
echo $array['Duration'];
echo "</td>";
echo "</tr>";
}
NOTE the use of single-quotes, ', inside the double-quotes - and the '" / "' surrounding the variable.
Alternatively, your echo with the link could look like this:
echo "<a href='recordInfo.php?record='{$array['RecordNumber']}'>{$array['RecordNumber']}</a>";
These accomplish the same thing.
This principle is the same, BTW, if your code looks like this:
$results = some_mysql_query;
while ($row = mysql_fetch_array($result)) {
...
echo $row['RecordNumber'];
Also, in case you're not already aware of how this will work, your recordInfo.php will receive its information in the $_GET array; specifically, it will refer to the RecordNumber as $_GET['RecordNumber'].
I've successfully retrieved car make and car name accordingly but I've no idea on how to retrieve image for that particular car.I tried using nested foreach but didn't work as I expected.Instead of displaying particular image for a car id ,it shows the same image from last folder for all ids.
here's my code :
while($row = mysql_fetch_array($result_name,MYSQL_ASSOC)) {
$car[$row['carMake_id']][] = $row['carName'];
//$car_name_id=$row['carName_id'];
$gallery=$row['gallery'];
//$car_make_id=$row['carMake_id'];
//$car_gallery[$row['carName_id']][]=$row['gallery'];
}
foreach ($car as $carmake => $carname) {
echo "<tr><td style='background-color:#0066cc;'><b>".$carmake ."</b></td></tr><tr>";
foreach ($carname as $title) {
echo "<td>".$title . "<br/> ";
?>
//this part displays image.. I want it to display according to the car name ($title)
<img src="management/uploads/<?php echo $carmake;?>/<?php echo $gallery;?>" width="100" height="100"></td>
<?php
}
echo'</tr>';
}
Also how do I retrieve all other information pertaining to a particular car inside the foreach?
Thanks.
EDITED PART:
$gallery[]=$row['gallery'];//inside while loop
foreach ($car as $carmake => $carname)
{
echo "<tr><td style='background-color:#0066cc;'><b>".$carmake ."</b></td></tr><tr>";
foreach ($carname as $title) {
echo "<td>".$title . "<br/> ";
foreach($gallery as $g)//new foreach to retreive images
{
echo $g;
}
?>
<img src="management/uploads/<?php echo $carmake;?>/<?php echo $g;?>" width="100" height="100"></td>
You need to put gallery into the array.
<?php
while($row=mysql_fetch_array($result_name,MYSQL_ASSOC))
{
$car[$row['carMake_id']][] = $row;
}
foreach ($car as $carmake => $carname)
{
echo "<tr><td style='background-color:#0066cc;'><b>".$carmake ."</b></td></tr><tr>";
foreach ($carname as $title)
{
echo "<td>{$title['carName']}<br/> ";
//this part displays image.. I want it to display according to the car name ($title)
echo "<img src='management/uploads/$carmake/{$title['gallery']}' width='100' height='100'></td>";
}
echo'</tr>';
}
?>
I have tried the following code:
$car_row = $car_xpath->query('//h3[#class="adtitlesnb"]');
$car_row2 = $car_xpath->query('//div[#class="snb_price_tag"]');
$i = 0;
echo "<table><thead><tr><td>Car Name</td><td>Price</td></tr></thead><tbody>";
foreach($car_row as $row){
echo "<tr><td>";
echo $row->nodeValue;
echo "</td><td>";
echo $car_row2->nodeValue;
echo "</td></tr>";
}
echo "</tbody></table>";
You can't iterate over one array with foreach and expect the other to follow.
foreach essentially resets the current index on an array, then loops through calling next on the array until there are no elements left. Using reset and next you can emulate this for your second array as follows:
$car_row = $car_xpath->query('//h3[#class="adtitlesnb"]');
$car_row2 = $car_xpath->query('//div[#class="snb_price_tag"]');
echo "<table><thead><tr><td>Car Name</td><td>Price</td></tr></thead><tbody>";
$row2 = reset($car_row2); // set the internal array pointer to the begining
foreach($car_row as $row) {
echo "<tr><td>";
echo $row->nodeValue;
echo "</td><td>";
echo $row2->nodeValue;
echo "</td></tr>";
$row2 = next($car_row2); // retrieve the next node from car_row2
}
echo "</tbody></table>";