Echo different values in the same column in the same table - php

I'm trying to display the values from two different rows into two columns in a single row but I can't figure out how to retrieve the data.
This is what I tried:
SELECT wp_posts.*, wp_postmeta.*
FROM wp_posts
LEFT JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_posts.post_type='product' AND wp_postmeta.meta_key='_sale_price'
OR wp_postmeta.meta_key='_stock'
ORDER BY wp_posts.ID
I display the data like this:
foreach($sth as $row)
{
?>
<tr>
<td><?php echo $row["ID"]; ?></td>
<td><?php echo $row["post_title"]; ?></td>
<td><?php echo $row["meta_value"]; ?></td>
<td><?php echo $row["meta_value"]; ?></td>
</tr>
I'm getting this:
While I'd like to get this:
I'm not sure whether I should post this in the PHP, WP or SQL category.

use this code:
only replace your tables with this example
$sql = "SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName";
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_assoc($result)) {
<tr>
<td><?php echo $row["ID"]; ?></td>
<td><?php echo $row["post_title"]; ?></td>
<td><?php echo $row["meta_value"]; ?></td>
<td><?php echo $row["meta_value"]; ?></td>
</tr>
///your code here
}

It seems that your fetch results are coupled, so I'd suggest that you print the fetch results in the rule below:
/* Print this when $row is in odd number */
<tr>
<td><?php echo $row["ID"]; ?></td>
<td><?php echo $row["post_title"]; ?></td>
<td><?php echo $row["meta_value"]; ?></td>
/* Print this when $row is in even number */
<td><?php echo $row["meta_value"]; ?></td>
</tr>
In the end, according to the sample data you provide above, you will find that the 1st foreach it prints
"#" = '285'
"Nom" = 'Bouquet Opalin'
"Prix HT" = '25'
2nd foreach it prints:
"Stock" = '9'
I did the example for you, click the link below.
For your reference
/* added at 2018-04-26 */
<?php
/* include your dbconfig */
include_once('../sit/dbConfig.php');
$dbConfig = new dbConfig();
$query = array();
$query[0] = "SELECT * FROM wp_posts WHERE 1;";
$column = " ID, post_title, meta_value ";
$table = " wp_posts ";
$arr = array();
$arr = $dbConfig->sqlSelect1($column, $table);
echo "<pre>";
echo "1) Print raw array data which get from DB" . "<br><br>";
print_r( $arr );
echo "</pre>";
echo "<br><br>";
?>
<?php
echo "<pre>";
echo "2) Print in table format" . "<br><br>";
echo "<table border='1' width=device-width >";
echo "<tr>";
echo "<td>#</td>";
echo "<td>Nom</td>";
echo "<td>Prix HT</td>";
echo "<td>Stock</td>";
echo "</tr>";
/* Print this when $row is in even number */
foreach ($arr as $row => $key) {
if ($row % 2 == 0) {
echo "<tr> <td>";
echo $key["ID"];
echo "</td> <td>";
echo $key["post_title"];
echo "</td> <td>";
echo $key["meta_value"];
echo "</td>";
}
/* Print this when $row is in odd number */
if ($row % 2 == 1) {
echo "<td>";
echo $key["meta_value"];
echo "</td> <tr>";
}
}
echo "</table>";
echo "</pre>";
?>

Related

How to fix JSON Data not populating in HTML Table

I built an application that outputs json data showing who represents you in congress, using php. I created a table to output the data however, it's not rendering, not sure where the error is?
<div class="data-table-wrapper">
<?php
$json_rep =
file_get_contents('https://whoismyrepresentative.com/getall_mems.php?zip='.$_GET['zip'].'&output=json');
$rep_array = json_decode($json_rep);
var_dump($rep_array);
?>
<table class="data-table">
<thead>
<tr>
<td>Name</td>
<td>Party</td>
<td>State</td>
<td>District</td>
<td>Phone</td>
<td>Office</td>
<td>Link</td>
</tr>
<?php
foreach($rep_array->results->Name->Party as $key=>$item){
?>
<tr>
<td><?php echo $item->name; ?></td>
<td><?php echo $item->party; ?></td>
<td><?php echo $item->state; ?></td>
<td><?php echo $item->district; ?></td>
<td><?php echo $item->phone; ?></td>
<td><?php echo $item->office; ?></td>
<td><?php echo $item->link; ?></td>
</tr>
<?
}
?>
</table>
</div>
</div>
should render key and value pair into the table?
Use $rep_array = json_decode($json_rep, true); to get an associative array back. Now all you need to do is loop through the array to render any of the data in your table, like so:
<?php
$json_rep = file_get_contents('https://whoismyrepresentative.com/getall_mems.php?zip=10001&output=json');
$rep_array = json_decode($json_rep, true);
echo "<table>";
foreach ($rep_array['results'] as $key => $value) {
foreach ($value as $key1 => $value1) {
echo "<tr>";
echo "<td>";
echo $key1;
echo "</td>";
echo "<td>";
echo $value1;
echo "</td>";
echo "</tr>";
}
}
echo "</table>";

vertical column in table Display query result horizontally in html table

i want to display vertical values of the field , horizontally in html table.
my table structure is like
ID Category Value (ID is Foriegn Key)
1 A 23
1 B 25
.
.
.
.
1 S 30
2 A 10
2 B 11
.
.
.
2 S 22
ID Area_A Area_B Area_C (ID is primary key)
i want to join these table and want to display query result horizontally like
ID Area_A Area_B Area_C A B ........ S
1 aa bb cc 23 25 30
can anyone help me?
is this possible ?
$sql = "select * from tbl1, tbl2 where tble1.ID = tble2._ID ";
$res = mysqli_query($con, $sql);
while($row = mysqli_fetch_object($res)) {
?>
<tr>
<td><?php echo "$row->ID"; ?></td>
<td><?php echo "$row->Area_A"; ?></td>
<td><?php echo "$row->Area_B"; ?></td>
<td><?php echo "$row->Area_C; ?></td>
<td><?php echo "$row->Value "; ?></td>
<td><?php echo "$row->Value"; ?></td>
<td><?php echo "$row->Value"; ?></td>
<td><?php echo "$row->Value"; ?></td>
<td><?php echo "$row->Value"; ?></td>
<td><?php echo "$row->Value"; ?></td>
<td><?php echo "$row->Value"; ?></td>
<td><?php echo "$row->Value"; ?></td>
<td><?php echo "$row->Value"; ?></td>
<td><?php echo "$row->Value"; ?></td>
<td><?php echo "$row->Value"; ?></td>
<td><?php echo "$row->Value"; ?></td>
<td><?php echo "$row->Value"; ?></td>
<td><?php echo "$row->Value"; ?></td>
<td><?php echo "$row->Value"; ?></td>
</tr>
<?php
but the problem is inserted data like this
this black ellipse should display horizontally and only once
Your php code in your example creates a row in your HTML table for every row in your result set from MySQL. Change that code to create columns. php is excellent for this sort of thing; it manipulates text and HTML tables are just text.
To do this you need to, actually, create two rows. One is the header row and the other the detail row.
Try something like this. Please notice that this code is not debugged.
$hdr = "";
$dat = "";
while($row = mysqli_fetch_object($res)) {
$hdr = $hdr + "<td>" + $row->Category + "</td>";
$dat = $dat + "<td>" + $row->Value + "</td>";
}
echo "<tr>" + $hdr + "</tr>";
echo "<tr>" + $dat + "</tr>";
Do you see how $hdr = $hdr + table cell accumulates a bunch of table cells for each data item, containing the Category values in your result set? Likewise, $dat = $dat + table cell accumulates a bunch of table cells for each data item.
Sorry to say I don't understand Area_A and the other area items in your question, so I haven't tried to program those.

How to display a values in code-igniter table from controller?

I have a one array in code-igniter controller
foreach($array as $row) {
echo $row['_source']['shape'];
echo "<br>";
echo $row['_source']['cut'];
echo "<br>";
echo $row['_source']['color'];
echo "<br>";
echo $row['_source']['clarity'];
echo "<br>";
echo $row['_source']['lab'];
echo "<br>";
echo $row['_source']['polish'];
echo "<br>";
echo $row['_source']['symmetry'];
echo "<br>";
echo $row['_source']['stone_id'];
echo "<br>";
echo $row['_source']['fluorescence'];
echo "<br>";
echo $row['_source']['cert_no'];
echo "<br>";
echo $row['_source']['location'];
echo "<br>";
}
which gives me two different bunch of values
In first bunch of values i got
round 3X D IF GIA VG G id01 FNT xy01 india
In second bunch of values i got
heart 2X f IF IGI VG G id01 FNT xy01 china
Now i want to display this all values in table in view part..
First send your data to view as follows:
//Retrive your data here and put in $result
$data['res']=$result;
$this->load->view('view_page', $data);
Then in your view page
<table>
<?php
foreach($res as $row) {
echo "<tr>";
echo "<td>".$row['_source']['shape']."</td>";
echo "<td>".$row['_source']['cut']."</td>";
echo "<td>".$row['_source']['color']."<td>";
echo "<td>".$row['_source']['clarity']."</td>";
echo "<td>".$row['_source']['lab']."</td>";
echo "<td>".$row['_source']['polish']."</td>";
echo "<td>".$row['_source']['symmetry']."</td>";
echo "<td>".$row['_source']['stone_id']."</td>";
echo "<td>".$row['_source']['fluorescence']."</td>";
echo "<td>".$row['_source']['cert_no']."</td>";
echo "<td>".$row['_source']['location']."</td>";
echo "</tr>";
}
?>
</table>
In controller :
$data['your_array'] = $array;
$this->load->view('view_page', $data);
View page :
<table>
<?php
if(is_array($values) && count($your_array) > 0) {
foreach($your_array as $row) {
?>
<tr>
<td><?php echo $row['_source']['shape']; ?></td>
<td><?php echo $row['_source']['cut']; ?></td>
<td><?php echo $row['_source']['color']; ?><td>
<td><?php echo $row['_source']['clarity']; ?></td>
<td><?php echo $row['_source']['lab']; ?></td>
<td><?php echo $row['_source']['polish']; ?></td>
<td><?php echo $row['_source']['symmetry']; ?></td>
<td><?php echo $row['_source']['stone_id']; ?></td>
<td><?php echo $row['_source']['fluorescence']; ?></td>
<td><?php echo $row['_source']['cert_no']; ?></td>
<td><?php echo $row['_source']['location']; ?></td>
</tr>
<?php
}
}
?>
</table>

linking an item in table using php

I am trying to figure out how to link items in a row of a table while keeping the row in place, I have the following code:
echo "<br><br><table border=0 cellpadding=3>";
echo "<td><b>Player Name</b></td>";
echo "<td><b>Position</b></td>";
echo "<td><b>Height</b></td>";
echo "<td><b>Weight</b></td>";
echo "<td><b>Birthdate</b></td>";
echo "<td><b>NHL Rights</b></td>";
echo "<td><b>CNGHL Team</b></td>";
echo "<td><b>Current Team</b></td>";
echo "<td><b>Current League</b></td>";
while($row = mysql_fetch_array($oteaminfo))
{
echo "<tr>";
echo "<td>".$row['FullName']."</td> ";
echo "<td>".$row['Position']."</td> ";
echo "<td>".$row['Height']."</td> ";
echo "<td>".$row['Weight']."</td> ";
echo "<td>".$row['DOB']."</td> ";
echo "<td>".$row['Team']."</td> ";
echo "<td>".$row['CNGHLRights']."</td> ";
echo "<td>".$row['InternationalTeam']."</td> ";
echo "<td>".$row['InternationLeague']."</td> ";
echo "</tr>";
}
echo "</table>";
I have tried using
echo "<a href=\"cnghlplayers.php? PlayerID=".$row['PlayerID']."\">".$row['FullName']."<br>";
In Place of the
echo "<td>".$row['FullName']."</td> ";
In the table but It puts the links into a new row above my current table. Any help would be greatly appreciated, I tried searching for this topic but I couldn't find any information that was helpful.
Thanks!
Just wrap the <td> element around:
echo '<td>'.$row['FullName'].'</td>';
Wrap your link with TD:
echo "<td>".$row['FullName']."</td>";
Test this, there were some issues with your table markup. This should be a bit easier to read as well.
?> <!-- this line ends your php code so that you can format the HTML sanely -->
<br />
<br />
<table border=0 cellpadding=3>
<tr>
<td><b>Player Name</b></td>
<td><b>Position</b></td>
<td><b>Height</b></td>
<td><b>Weight</b></td>
<td><b>Birthdate</b></td>
<td><b>NHL Rights</b></td>
<td><b>CNGHL Team</b></td>
<td><b>Current Team</b></td>
<td><b>Current League</b></td>
</tr>
<?php while($row = mysql_fetch_array($oteaminfo)) { ?>
<tr>
<td><?php echo "".$row['FullName'].""; ?></td>
<td><?php echo $row['Position']; ?></td>
<td><?php echo $row['Height']; ?></td>
<td><?php echo $row['Weight']; ?></td>
<td><?php echo $row['DOB']; ?></td>
<td><?php echo $row['Team']; ?></td>
<td><?php echo $row['CNGHLRights']; ?></td>
<td><?php echo $row['InternationalTeam']; ?></td>
<td><?php echo $row['InternationLeague']; ?></td>
</tr>
<?php } ?>
</table>
<?php //continues your php code below

MySQL Fetch Array Each Row Function

This is the first time I'm using this website, I have recently started working on a project where the webpage will display two table, one of the table will gather all the information from a SQL Database and place them in the table along with a button next to each rows allowing you to insert that record to another to the other table, so far, this is the code I have written:
//Include the Following Files
include 'connect.php';
//Select All Price Plans
$query = "SELECT * FROM pricing";
$result = mysql_query($query);
//Print all Plans in Table
while ($pricing = mysql_fetch_array($result))
{
echo "<table border=1>";
echo "<tr>";
echo "<td>" .$pricing['planID']. "</td>";
echo "<td>" .$pricing['planTitle']. "</td>";
echo "<td>" .$pricing['planDescription']. "</td>";
echo "<td>" .$pricing['planPricing']. "</td>";
echo "<td>" . **BUTTON HERE** . "</td>";
echo "</tr>";
echo "<table>";
}
//Assign $link to Case
$link = '?run=planA';
//Pre-defined Functions
function planA()
{
$query = "INSERT into restaurant_plan (`planID`, `planTitle`, `planDescription`, `planPricing`) SELECT * FROM pricing WHERE planID='2' ";
$result = mysql_query($query);
}
//Setting Case to Run Each Functions
if (isset($_GET['run'])) $linkchoice=$_GET['run'];
else $linkchoice='';
switch($linkchoice)
{
case 'planA':
planA();
break;
case 'planB':
planB();
break;
}
Could anyone suggest any guide or possibly an example how I could assign a function to each rows? Thanks a lot!
your code prints one table for each record in table "pricing", use this code instead :
//Select All Price Plans
$mysqli = new mysqli("hostname", "username", "pass", "dbname");
$query = "SELECT * FROM pricing";
$result = $mysqli->query($query);
//Print all Plans in Table
echo "Available Price Plans";
echo "<table border=1>";
while ( $pricing = $result->fetch_assoc() ) {
echo "<tr>";
echo "<td>" .$pricing['planID']. "</td>";
echo "<td>" .$pricing['planTitle']. "</td>";
echo "<td>" .$pricing['planDescription']. "</td>";
echo "<td>" .$pricing['planPricing']. "</td>";
echo "<td>" .'<img style="border:none;" src="'. $icon .'" />'. "</td>";
//print a button as you mentioned
echo "<td>"."<form action='#' method='get'><input type='hidden' name='id' value='".$pricing['planID']."'/><input type='submit' value='copy'/></form></td>";
echo "</tr>";
}
echo "</table>";
and your function :
function planA()
{
// get selected plan info
$query = "SELECT * FROM pricing";
$result = $mysqli->query($query);
$row = $res->fetch_assoc();
//copy info to table 'restaurant_plan'
$query = "INSERT into restaurant_plan (".$_GET["id"].", ".$row["planTitle"].", ".$row["planDescription"].", ".$row["planPricing"].")";
$result = $mysqli->query($query);
}
Not an answer, but if you want your code to be cleaner & more readable/maintainable, you should use HTML with PHP inserted, rather than echoing HTML via PHP:
<?php
//Select All Price Plans
$query = "SELECT * FROM pricing";
$result = mysql_query($query);
//Print all Plans in Table
?>
Available Price Plans
<?php while ($pricing = mysql_fetch_array($result)) : ?>
<table border=1>
<tr>
<td><?= $pricing['planID'] ?></td>
<td><?= $pricing['planTitle'] ?></td>
<td><?= $pricing['planDescription'] ?></td>
<td><?= $pricing['planPricing'] ?></td>
<td><img style="border:none;" src="<?= $icon ?>" /></td>
</tr>
<table>
<?php endforeach; ?>
Also, as I mentioned in the comments above, you should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article.
Change your table output to the following:
<table border="1">
<?php
while ($pricing = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $pricing['planID']; ?></td>
<td><?php echo $pricing['planTitle']; ?></td>
<td><?php echo $pricing['planDescription']; ?></td>
<td><?php echo $pricing['planPricing']; ?></td>
<td><a href='<?php echo $link; ?>'><img style="border:none;" src='<?php echo $icon; ?>' /></a></td>
</tr>
<?php
}
?>
<table>
Double check you database connection as well to ensure data can be retrieved.

Categories