Im writing a function to get the parent and child ids but for the third loop there is a
problem the loop gets even the previous loops id also .
How can i avoid it?
<?
$results = '
<table>
<thead>
<tr >
<td id="ticket" align="center" ><b>Task<br />ID</b></td>
<td id="ticket" align="center" ><b>col1</td>
<td id="ticket" align="center" ><b>col2</td>
</tr>
</thead>
<tbody>';
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{
$results .='
<tr >
<td align="center">
'.$row['Task_id'].'
</td>';
$results .= '<td align="center">';
$gg = mysqli_query($dbc,"select * from Tasks where ParentTask_Id='".$row['Task_id']."'");
echo "<br>";
while ($rowdd = mysqli_fetch_assoc($gg))
{
$results .= $rowdd['Task_id']."<br><br>";
$gg2 = mysqli_query($dbc,"select * from Tasks where ParentTask_Id='".$rowdd['Task_id']."'");
while ($rowdd2 = mysqli_fetch_assoc($gg2))
{
$results2 = $rowdd2['Task_id']."<br><br>";
}
echo "<br>";
}
// $results .= $car ;
// $results .= $t;
$results .='</td>';
$results .=' <td align="left" >'?>
<?
$results .= $results2;
$results .='</td>';
$results .='
</tr>';
}
?>
Is the $results variable empty? I only see it being concatenated.
Also, on your table you have multiple ids that are the same. You either need to change that to a class or have a unique value for each id.
Related
I am fetching all the records from mysql but it split in to rows as shown in the image
I want that it display row wise means the column which are splitting in the column means "future of india" and GAURAV should come in line now row wise. When the body part full it auto split into new row. here is my code
$sql = "SELECT * FROM category order by id ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo '<table width="30%" border="0">
<tr>
<td><div id="rcorners2">'.$row["category"].' </div></td>
</tr>
</table>
Use <span> instead of<div> in the <td> and it will work
You need to move your loop.
$sql = "SELECT * FROM category order by id ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
echo '<table width="30%" border="0">
while($row = mysqli_fetch_assoc($result)) {
<tr>
<td>
<div id="rcorners2">'.$row["category"].' </div>
</td>
</tr>";
}
echo "</table>";
You should take table wrap all rows.
<?php
$sql = "SELECT * FROM category order by id ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
echo '<table width="30%" border="0">'
while($row = mysqli_fetch_assoc($result)) {
echo '
<tr>
<td><div id="rcorners2">'.$row["category"].' </div></td>
</tr>';
}
echo '</table>';
}
?>
If I understand correctly, I'll write pseudocode
<table>
<tr>
$query = "..."
while ($row = fetch()) {
<td>
print row data
</td>
}
</tr>
</table>
I am using mysql queries to fetch data from db. All my data are showing fine in tables. Now I want to color the status by value less than 2 or more than 3.
The below code is not working.
Need help.
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>name</th>
<th>genre</th>
<th>time</th>
<th>status</th>
<th>more...</th>
</tr>';
?>
<?php
function status_style($row) {
if ($row < 2) return 'background-color: #ff0000'; //red
if ($row > 3) return 'background-color: #33cc33'; //green
return '';
}
?>
<?php
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td>'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
}
echo $output;
{
echo 'Data Not Found';
}
?>
You have already created function to color the rows but haven't called the function during showing output. That's why it is not working.
Change your $output variable with:
$output .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td style="'.status_style($row["status"]).'">'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
You can try something like this:
<?php
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>name</th>
<th>genre</th>
<th>time</th>
<th>status</th>
<th>more...</th>
</tr>';
function status_style($row) {
if ($row < 2) return $color = '#ff0000'; //red
if ($row > 3) return $color = '#33cc33'; //green
return $color = '';
}
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr style='"'background-color: '"' . $color . '"'>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td>'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
}
echo $output;
{
echo 'Data Not Found';
}
You didn't do anything with the background color.
HTML Table getting mixed up when having echo in while loop. Bellow attached the output image for that. How can i get normal table with loop result? Please note i am getting table data by searching with manual date.
<?php
if (isset($_POST['submit_date'])) {
if (empty($_POST['m_date'])) {
echo '<div class="alert alert-danger">Error: Select date then search</div>';
}else{
$m_date = $_POST['m_date'];
$q = mysqli_query($conn, "SELECT * FROM bazar_dor WHERE m_date='$m_date'");
echo '<table style="width:100%">
<tr>
<th>Category Name</th>
<th>Price</th>
</tr>
<tr>
';
while ($row=mysqli_fetch_array($q)) {
$cat_name = $row['cat_name'];
$price = $row['price'];
echo '<td>'.$cat_name.'</td><td>'.$price.'</td>';
}
echo '</tr></table>';
}
}
?>
the output i am getting from this code
It looks like you'll need to include table rows (<tr>) inside your loop, as well. Your current code outputs all data on the same row, which undoubtedly breaks the table.
echo '<table style="width:100%">
<tr>
<th>Category Name</th>
<th>Price</th>
</tr>';
while ($row=mysqli_fetch_array($q)) {
$cat_name = $row['cat_name'];
$price = $row['price'];
echo '<tr><td>'.$cat_name.'</td><td>'.$price.'</td></tr>';
}
echo '</table>';
Replace your cede with this
<?php if (isset($_POST['submit_date'])) { if (empty($_POST['m_date'])) { echo '<div
class="alert alert-danger">Error: Select date then search</div>'; }else{ $m_date = $_POST['m_date'];
$q = mysqli_query($conn, "SELECT * FROM bazar_dor WHERE m_date='$m_date'");
echo '<table style="width:100%"> <tr>
<th>Category Name</th> <th>Price</th> </tr> ';
while ($row=mysqli_fetch_array($q)) { $cat_name = $row['cat_name'];
$price = $row['price']; echo '<tr><td>'.$cat_name.'</td><td>'.$price.'</td><\tr>';
} echo '</table>';
} } ?>
Can someone help me, how to code the logic to print the data from my database into a <table>?
<table border="1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM myTable" );
foreach ( $result as $print ) {
echo '<td>' $print->firstname.'</td>';
}
?>
</tr>
</table>
I know this is so basic, but I'm having a hard time making this work.
Try this:
<table border="1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM myTable" );
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->firstname;?></td>
</tr>
<?php }
?>
You're missing . in echo '<td>' $print->firstname.'</td>';
Try this
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM myTable" );
foreach ( $result as $print ) {
echo '<tr>';
echo '<td>' . $print->firstname.'</td>';
echo '<td>' . $print->lastname.'</td>';
echo '<td>' . $print->points.'</td>';
echo '</tr>';
}
?>
Try this:
$result = $wpdb->get_results("SELECT * FROM myTable" , ARRAY_A); //get result as associative array
Then the usual cycle:
//spare memory
$count = count($result);
//fastest way to perform the cycle
for ($i = $count; $i--;) {
echo '<td>'. $print[$i]['firstname'].'</td>';
}
You just need to put <tr> inside your foreach loop, and add . concatenation operator in your line, you need also try this :
You need to wrap <td></td> inside <tr></tr> in foreach loop
You need to add . concatenation operator in line that contains firstname variable.
If you have duplicate values, then add this parameter ARRAY_A to your query
$result = $wpdb->get_results ( "SELECT * FROM myTable",ARRAY_A );.
<table border="1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM myTable" );
foreach ( $result as $print ) {
echo '<tr>';
echo '<td>' . $print->firstname .'</td>';
echo '<td>' . $print->lastname .'</td>';
echo '<td>' . $print->points .'</td>';
echo '</tr>';
}
?>
</table>
I have set up an 'orders' page that should be pretty straight-forward, requiring a for each loop to generate past orders from the DB. Within each loop, there needs to be an explode to separate each item(,), then another explode to pull the details out of each item(-). I was wondering if anyone could tell me why I'm getting the error message "Warning: mysql_num_rows() expects parameter 1" at the end of each iteration of my foreach loop? It's particularly confusing for me because the code still works, which I don't think it should if the error was true.
<?php
$sql = "SELECT * FROM orders WHERE store='$user'";
$res = mysql_query($sql);
$arrOrders = array();
while ($row = mysql_fetch_array($res)) {
array_push($arrOrders, $row);
}
?>
<table width="85%" align="center" border="0" cellpadding="5">
<?php if (count($arrOrders) > 0) { ?>
<?php foreach ($arrOrders as $key => $value) { ?>
<tr>
<td valign="top" style="font-weight: bold">
ID #<?=$value['id']?>
</td>
<td valign="top" style="font-weight: bold">
Status: <?=$value['status']?>
</td>
<td valign="top" style="font-weight: bold">
Order #<?=$value['order_ref']?>
</td>
<td align="left" valign="top" style="font-weight: bold">
<?php
$tmpProds = explode(',', $value['products']);
foreach ($tmpProds as $key2 => $value2) {
$tmpProdInfo = explode('-', $value2);
$sql2 = 'SELECT * FROM products_full WHERE id = ' . $tmpProdInfo[0];
$res2 = mysql_query($sql2);
if (mysql_num_rows($res2) > 0) { // THIS IS THE ROW THAT THE ERROR MESSAGE POINTS TO
echo $tmpProdInfo[1] + $tmpProdInfo[2] . ' x ' . mysql_result($res2, 0, 'alpha_code') . ' (' . trim(mysql_result($res2, 0, 'description')) . ')<br /><br />';
}
}
?>
</td>
</tr>
<tr>
<td width="100%" colspan="5" align="center">
<hr style="width: 100%" />
</td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td width="100%" align="center">
There are currently no complete orders.
</td>
</tr>
<?php } ?>
</table>
Thanks in advance, I think I've included everything that is relative but if anything else is needed please let me know.
Joe
Try this:
$sql2 = 'SELECT * FROM products_full WHERE id = '.$tmpProdInfo[1].'';
if (count($res2) > 0)
Also, be aware when you want to use MySQL_num_rows you should include the connection. see the manual:
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);