I would need to display results in 3 rows and have same values under each other. Currently results are all going under each other. My code would have 3 IF sentences inside the loop.
My code is here:
echo "
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\" class=\"data\">
<tr>
<td class=\"dataHeader\"><h2>Vapaa</h2></td>
<td class=\"dataHeader\"><h2>Varattu</h2></td>
<td class=\"dataHeader\"><h2>Käyttö estetty</h2></td>
</tr>
";
while($result = mysql_fetch_array($sql)) {
echo "<tr>
if ($result[tila] == 'vapaa') {
<td>" . $result['id'] . " " . $result['tila']. "</td>
}
</tr>";
}
echo "</table>";
I think you don't want the if to be as text. So you have to do like this:
while($result = mysql_fetch_array($sql)) {
echo "<tr> ";
//First if
if ($result[tila] == 'vapaa') {
echo "<td>" . $result['id'] . " " . $result['tila']. "</td>";
} else {
echo '<td></td>';
}
//Second if
if ($result[tila] == 'varattu') {
echo '<td>' , $result['id'] , ' ' , $result['tila']. '</td>';
} else {
echo '<td></td>';
}
// Here you can add more ifs.
echo "</tr>";
}
Because tables need to have the same number of cells (<td>) in a you need to add empty <td>s as describe above.
Wrong use of string quote delimiters.
echo '<table>';
while($result = mysql_fetch_array($sql)) {
echo '<tr>';
if ($result[tila] == 'vapaa') {
echo '<td>' , $result['id'] , ' ' , $result['tila']. '</td>';
}
echo '</tr>';
}
echo '</table>';
Related
I can't seem to get this figured out. What am I doing wrong? I have a table in my database and some rows have an empty column for the "company" field.
I'm trying to get the "Users own uploaded files in the html table below. It belongs with "1629"
<?php
// Make a MySQL Connection
mysql_connect("localhost", "", "") or die(mysql_error());
//echo "Connected to MySQL<br />";
mysql_select_db("") or die(mysql_error());
//echo "Connected to Database";
$query = "SELECT company, dmy, COUNT(company) FROM AdTracking WHERE DATE(dmy) = CURRENT_DATE GROUP BY company";
$result = mysql_query($query) or die(mysql_error());
echo "<div style='margin-top:100px;'><center><h2>";
echo date('l jS \ F Y');
echo "<br />";
echo "</h2><center></div>";
echo '
<center> <table class="pure-table pure-table-horizontal">
<thead>
<tr>
<th>Company</th>
<th>Total</th>
</tr>
</thead>
<tbody>
';
// Print out result
while($row = mysql_fetch_array($result)){
if ($row['company'] == NULL) {
echo "Users own uploaded files";
};
echo "<tr>";
echo "<td><strong>" . $row['company'] . "</strong></td>";
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "</tr>";
}
echo '
</tbody>
</table> </center>
';
?>
You are printing outside of td and tr.
You have maintain format .
Try like this
echo "<tr>";
echo "<td><strong>" .($row['company'] == NULL ? "Users own uploaded files" : $row['company']). "</strong></td>";
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "</tr>";
you should use mysql_fetch_assoc() instead of mysql_fetch_array()
while($row = mysql_fetch_array($result)){
if (!$row['company']) // it check null or have something value
$value = "Users own uploaded files";
else
$value = $row['company'];
echo "<tr>";
echo "<td><strong>" . $value . "</strong></td>";
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "</tr>";
}
use mysqli or PDO instead of mysql because "mysql" is deprected.
Maybe someone can help me out. Any help is appreciated! I'm trying to Combine the "???" row with "unknown files" row. So the total would be 4245. Just one row.
I'm using a while loop. Here is my code
<?php
// Make a MySQL Connection
mysql_connect("localhost", "", "") or die(mysql_error());
//echo "Connected to MySQL<br />";
mysql_select_db("") or die(mysql_error());
//echo "Connected to Database";
$query = "SELECT company, username, COUNT(company), username FROM AdTracking WHERE DATE(dmy) = CURRENT_DATE GROUP BY company ORDER BY company ASC";
$result = mysql_query($query) or die(mysql_error());
echo "<div style='margin-top:100px;'><center><h2>";
echo date(' \ F jS Y - l');
echo "<br />";
echo "</h2><center></div>";
echo '
<center> <table class="pure-table pure-table-horizontal">
<thead>
<tr>
<th>Company</th>
<th>Total</th>
<th>Users</th>
</tr>
</thead>
<tbody>
';
// Print out result
while($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td><strong>" .($row['company'] == NULL ? "???" : $row['company']). "</strong></td>";
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "<td> ... </td>";
echo "</tr>";
}
echo '
</tbody>
</table> </center>
';
?>
You will have to calculate total of ??? and unknown file outside the while loop
$total = 0;
while($row = mysql_fetch_assoc($result))
{
if($row['company'] == NULL || $row['company'] == "unknown file")
$total += $row['COUNT(company)'];
}
Then you can use that total in the main output loop
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td><strong>" .($row['company'] == NULL ? "???" : $row['company']). "</strong></td>";
if($row['company'] == "unknown file")
echo "<td>" . $total . "</td>";
else
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "<td> ... </td>";
echo "</tr>";
}
i want to fill two different html tables inside php while loop .My current code populate table 1 when name is not empty($row[name]) . However , how i can declare table2 so it only populate table2 when name is empty ? could any one show me how i can declare 2 different tables and fill both them depending if name value from db is empty or not ?
//table declration
echo "<table border='1'>
<tr>
<th>ID</th>
<th>name</th>
<th>page URL</th>
<th>img URL</th>
</tr>";
foreach ($lines as $line) {
//print_r( $line );
$line = rtrim($line);
$result = mysqli_query($con,"SELECT ID,name,imgUrl,imgPURL FROM testdb WHERE imgUrl like '%$line'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
//echo $result;
if(mysqli_num_rows($result)>0)
{
if(mysqli_num_rows($result)>1)
{
echo "<br>Duplicate in DB:".$line."<br>";
};
while($row = mysqli_fetch_array($result))
{
$totalRows++;
if (!empty($row[name]))
{
echo "<tr>";
echo "<td>" . $row['ID'] ."(".$totalRows. ")</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['imgPURL'] . "</td>";
echo "<td>" . $row['imgUrl'] . "</td>"; echo "</tr>";
}
else
{
$emptyNameCounter++;
//fill table2 if name is empty
};
}; // end of while loop
}
else
{
echo "<br>Not Found:".$line."<br>";
};
};// end of for each line loop
echo "</table>";
One other way is append your data in varables for both table one and two and after the loop put the data in tables
here is a demo how you can do that
$table1='';
$table2='';
while($row = mysqli_fetch_array($result))
{
$totalRows++;
if (!empty($row[name]))
{
$table1.="<tr>";
$table1.="<td>" . $row['ID'] ."(".$totalRows. ")</td>";
$table1.="<td>" . $row['name'] . "</td>";
$table1.="<td>" . $row['imgPURL'] . "</td>";
$table1.="<td>" . $row['imgUrl'] . "</td>"; echo "</tr>";
}
else
{
$emptyNameCounter++;
$table2.="data to show in table 2";
//fill table2 if name is empty
}
} // end of while loop
if(!empty($table1)){
echo "<table border='1'>
<tr>
<th>ID</th>
<th>name</th>
<th>page URL</th>
<th>img URL</th>
</tr>";
echo $table1;
echo "</table>";
}
if(!empty($table2)){
echo "<table border='1'>
<tr>
<th>ID</th>
<th>name</th>
<th>page URL</th>
<th>img URL</th>
</tr>";
echo $table2;
echo "</table>";
}
The easiest way is to collect the data in an array first, then loop over that array twice.
There are other variations on that theme, but basically they're all about splitting the reading from the database and the rendering into two distinct steps.
Basically something like:
while($row = mysqli_fetch_array($result)) $rows[] = $result;
foreach( $rows as $row ) {
// Render table 1
}
foreach( $rows as $row ) {
// Render table 2
}
Rather than echo the tables out, store each table to a variable. Once the loop is finished, echo out the variables. Example
$tableOne = "<table border='1'>
<tr>
<th>ID</th>
<th>name</th>
<th>page URL</th>
<th>img URL</th>
</tr>";
$tableTwo = "<table border='1'>
<tr>
<th>ID</th>
<th>name</th>
<th>page URL</th>
<th>img URL</th>
</tr>";
//Code and Query here
while($row = mysqli_fetch_array($result)) {
$totalRows++;
if (!empty($row[name])) {
$tableOne .= "<tr>";
$tableOne .= "<td>" . $row['ID'] ."(".$totalRows. ")</td>";
$tableOne .= "<td>" . $row['name'] . "</td>";
$tableOne .= "<td>" . $row['imgPURL'] . "</td>";
$tableOne .= "<td>" . $row['imgUrl'] . "</td>"; echo "</tr>";
}
else {
$emptyNameCounter++;
//store in $tableTwo if name is empty
}
}
//close tables, echo out variables
So this is my code but it does not work, I just get this
Title Article "; while($row = mysqli_fetch_array($result)) { echo ""; echo "" . $row['Title'] . ""; echo "" . $row['Article'] . ""; echo ""; } echo ""; mysqli_close($con); ?>
Any ideas?
<?php
$con=mysqli_connect("localhost","____","____","test_database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM written");
echo "<table border='1'>
<tr>
<th>Title</th>
<th>Article</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Title'] . "</td>";
echo "<td>" . $row['Article'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Tripple check your quotation marks are set correctly. Either
echo "<table border='1'>
or
</tr>";
has an error in it.
You may also try using echo as a function (bracelets), it should make spotting the error easier :-)
It would be helpfull to see the resulting markup instead of the text ;-)
You have to do this:
echo "<table border='1'><tr><th>Title</th><th>Article</th></tr>";
or
echo "<table border='1'>";
echo "<tr>";
echo "<th>Title</th>";
echo "<th>Article</th>";
echo "</tr>";
instead of
echo "<table border='1'>
<tr>
<th>Title</th>
<th>Article</th>
</tr>";
I want to set or assign id / rel inside my table code, take a look on my code below this so every table td has id and rel so I can give statement and manipulate the date within the jQuery, any helps will be much more appreciated:
function printdata()
{
if(is_array($_SESSION['chart']))
{
echo "Here's your chart" . "<br>";
$max = count($_SESSION['chart']);
$th1 = "<th>" . "No" . "</th>";
$th2 = "<th>" . "Nama Barang" . "</th>";
$th3 = "<th>" . "Harga Barang" . "</th>";
$th4 = "<th>" . "Stok Barang" . "</th>";
echo "<table border=1>";
echo "<tr>";
echo $th1 ;
echo $th2;
echo $th3;
echo $th4;
echo "</tr>";
for ($indexo = 0; $indexo < $max; $indexo++) {
echo "<tr>";
echo "<td>" . $indexo."</td>";
echo "<td >" . $_SESSION['chart'][$indexo]['namabarang']."</td>";
//i want to assign td rel inside of this echo
echo "<td>" . $_SESSION['chart'][$indexo]['hargabarang']."</td>";
echo "<td>" . $_SESSION['chart'][$indexo]['stokbarang']."</td>";
echo " <td><button id=".$indexo." name=\"button\">Edit</button></td>";
echo "</tr>";
}
echo "</table>";
}else
{
echo "Chart is still Empty";
}
}
echo "<table border=1 id=\"{$some_php_variable_with_an_id_value_in_it}\">"
first your code is too much line over there, that is no need to do such as that, it can be more simple to build some output with the same result, here may be could to help ya
function printdata(){
if(is_array($_SESSION['cart'])){
echo "Here's Your Chart ".count($_SESSION['cart'])."<br />";
echo '<table><tr>th>No</th><th>Nama Barang</th><th>Harga Barang</th><th>Stok Barang</th><th>Action</th></tr>';
for($indexo =0; $indexp < count($_SESSION['cart']); $indexo++){
echo "<tr>
<td id=".$indexo.">".$indexo."</td>
<td id=".$_SESSION['chart'][$indexo]['namabarang'].">".$_SESSION['chart'][$indexo]['namabarang']."<td>
<td id=".$_SESSION['chart'][$indexo]['hargabarang'].">".$_SESSION['chart'][$indexo]['hargabarang']."<td>
<td id="$_SESSION['chart'][$indexo]['stokbarang']">".$_SESSION['chart'][$indexo]['stokbarang']."<td>
<td><button id=".$indexo." name=\"button\">Edit</button></td>
</tr>";
}
}else{
echo "Chart is still Empty";
}
}
You can Change <td id=".$indexo."> to what ever you want to, seems like the other, id or rel will be the same implementation i guest