Split a table into three rows - php

I've got a PHP page, some of which is something like this:
if(mysql_num_rows($raw_results) > 0){
echo "<table border='1'>
<tr>
<th>C1</th>
</tr>";
while($row = mysql_fetch_array($raw_results))
{
echo "<tr>";
echo "<td>" . $row['C1'] . "</td>";
echo "</tr>";
}
echo "</table>";
How can I change this so the table goes C1 C1 C1?
So if I have the following set of values - 1 2 3 4 5 6 7 8 9 - the table returns the following
C1 C1 C1
1 2 3
4 5 6
7 8 9
How can I do this?

something like this:
$cpt=1;
echo "<tr>";
while($row = mysql_fetch_array($raw_results))
{
echo "<td>" . $row['C1'] . "</td>";
if ($cpt%3==0)
echo "</tr><tr>";
$cpt++;
}
echo "</tr>";
will create a new row after every 3 result

Define in $N how many cols you want.
$N=3;
$col=0;
while($row = mysql_fetch_array($raw_results))
{
$col++;
if ($col==1) echo "<tr>";
echo "<td>" . $row['C1'] . "</td>";
if ($col==$N)
{
$col=0;
echo "</tr>";
}
}
if ($col>0) //if there is an open <tr>...
echo "</tr>";

if(mysql_num_rows($raw_results) > 0) {
echo "<table border='1'>
<tr><th>C1</th><th>C1</th><th>C1</th></tr>";
$col = 0;
while($row = mysql_fetch_array($raw_results)) {
if ($col == 0) { echo "<tr>"; }
echo "<td>" . $row['C1'] . "</td>";
if ($col == 2) { echo "</tr>"; }
$col = ($col + 1) % 3
}
echo "</table>";
}

This is also Working (little bit short one)
$a=1;
while($row = mysql_fetch_array($raw_results))
{
echo "<td>".$row['c1']."</td>".(($a%3==0)?'</tr><tr>':'');
$a++;
}

Change it to
if(mysql_num_rows($raw_results) > 0){
echo "<table border='1'>
<tr>
<th>C1</th><th>C1</th><th>C1</th>";
$x = 0;
while($row = mysql_fetch_array($raw_results))
{
if ($x % 3 == 0) {
echo "</tr><tr>";
}
echo "<td>" . $row['C1'] . "</td>";
$x++;
}
echo "</table>";
I assume you have just 3 columns and count of your result is submultiple of 3
pay attention to </tr><tr>

No test so it might be some syntax error but you can do something like this:
if(mysql_num_rows($raw_results) > 0){
echo "<table border='1'>
<tr>
<th>C1</th>
<th>C1</th>
<th>C1</th>
</tr><tr>";
$i = 0;
while($row = mysql_fetch_array($raw_results))
{
if ($i == 2) {
$i = 0;
echo "</tr>";
echo "<tr>";
}
echo "<td>" . $row['C1'] . "</td>";
$i++;
}
if ($i > 0) {
echo "</tr>";
}
echo "</table>";

Related

Combine two rows together

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>";
}

A simple php script error result with tables

Hello guys i'm in need of php experts advice.
Here's my problem. A user has 2 inputs the start and the end this are all integers. this will able to identify odd and even. i have solve already odd and even and almost done. main problem ex. start value 1 end 5. 1 is odd it should be displayed on odd in table. but the problem is it is found in even table. initial value is the problem. the rest was good.
here's my code
<?php
$firstnum = $_POST['first_input'];
$secondnum = $_POST['second_input'];
$counter = 0;
echo "<table border='1'>";
if ($firstnum < $secondnum) {
echo "<tr>"; //first tr
echo "<th>"; echo "Even numbers"; echo "</th>";
echo "<th>"; echo "Odd numbers"; echo "</th>";
echo "</tr>";
for ($counter=$firstnum; $counter <= $secondnum ; $counter++) {
if ($counter % 2 == 0){
echo "<tr>";
echo "<td>";
echo $counter;
echo "</td>";
} else {
echo "<td>";
echo $counter;
echo "</td>";
echo "</tr>";
}
}
} elseif ($firstnum > $secondnum) {
# code...
//first num is < second num
echo "<tr>"; //first tr
echo "<th>"; echo "Even numbers"; echo "</th>";
echo "<th>"; echo "Odd numbers"; echo "</th>";
echo "</tr>";
for ($counter=$firstnum; $counter >= $secondnum ; $counter--) {
if ($counter % 2 == 0){
echo "<tr>";
echo "<td>";
echo $counter;
echo "</td>";
} else {
echo "<td>";
echo $counter;
echo "</td>";
echo "</tr>";
}
}
}
echo "</table>";
?>
Your issue is you have invalid html resulting from your if/else blocks.
If your if you have
<tr>
<td><td>
and in your else you have
<td></td>
</tr>
Both of these need full row/cell tags
<tr>
<td></td>
<td></td>
</tr>
So your code should look like
if ($counter % 2 == 0){
echo "<tr>";
echo "<td>";
echo $counter;
echo "</td>";
echo "<td>";
echo "</td>";
echo "</tr>";
} else {
echo "<tr>";
echo "<td>";
echo "</td>";
echo "<td>";
echo $counter;
echo "</td>";
echo "</tr>";
}

How to display 2 variables in a single cell Mysql

I would like to display two picture variables in a single cell
http://i.imgur.com/9ZuRhnD.gif
$result = mysqli_query($query)
or die(mysql_error());
if (mysqli_num_rows($result) > 0) {
echo "<table border='0' style='border-collapse: collapse;border-color: white;'>";
$i = 0;
while($row = mysqli_fetch_array($result)){
$Limg = $row['Limg'];
$Limgt = $row['Limgt'];
if ($i==0) {
echo "<tr>\n";
}
echo "<td align='center' width='140'>" . "<img src=\"{$row['Limgt']}\">" ."</td>";
echo "<td align='center' width='40'>" . "<img src=\"{$row['Limg']}\">" ."</td>";
$i++;
if ($i == $items) {
echo "</tr>\n";
$i = 0;
}
}
echo '</table>';
}else {
echo 'no records found';
}
?>
What am I missing
You're putting them each in their own cell () - try this:
$result = mysqli_query($query)
or die(mysql_error());
if (mysqli_num_rows($result) > 0) {
echo "<table border='0' style='border-collapse: collapse;border-color: white;'>";
$i = 0;
while($row = mysqli_fetch_array($result)){
$Limg = $row['Limg'];
$Limgt = $row['Limgt'];
if ($i==0) {
echo "<tr>\n<td>";
}
echo "<img src=\"{$row['Limgt']}\">";
echo "<img src=\"{$row['Limg']}\">";
$i++;
if ($i == $items) {
echo "</td></tr>\n";
$i = 0;
}
}
echo '</table>';
}else {
echo 'no records found';
}
?>
you are making them in two TDs so you have to make them in one TD like that
echo "<td align='center' width='140'>
<img src=\"{$row['Limgt']}\">
<img src=\"{$row['Limg']}\">
</td>";

How to show 4 names on 1 line instead of each name on new line in PHP?

Hi I have been using this code to fetch data from mysql database and displaying it in column format i.e
a
b
c
What i want is to show 4 names per row i.e
a b c d
e f g....
Can anybody help me in this regard
Thankyou.
Here is my code
<?php
$result = mysql_query("SELECT * FROM Persons");
echo "<table border='1'>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Use modulus operator %
$i = 0;
while($row = mysql_fetch_array($result))
{
$i++;
if ($i % 4 == 0) {
echo "<tr>";
}
echo "<td>" . $row['Name'] . "</td>";
if ($i % 4 == 0) {
echo "</tr>";
}
}
As a bonus, you've got syntax error in your code. Look at this line
echo "<table border='1'>;
which
should be
echo "<table border='1'>";
$cnt = 0;
while($row = mysql_fetch_array($result))
{
if($cnt%4==0) echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
$cnt++;
}
<?php
$con = mysql_connect("localhost","user","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
?>
<table border='1'>
<tr>
<?php
$count=1;
while($row = mysql_fetch_array($result))
{
if($count%4==0)
{ ?>
</tr><tr>
<?php }
<td><?php echo $row['Name']; ?></td>
$count++;
}
?>
</tr>
</table>
<?php
mysql_close($con);
?>

How to give alternating table rows different background colors using PHP

I have a table of data that is generated dynamically based on the contents stored in a mysql database.
This is how my code looks:
<table border="1">
<tr>
<th>Name</th>
<th>Description</th>
<th>URL</th>
</tr>
<?php
$query = mysql_query("SELECT * FROM categories");
while ($row = mysql_fetch_assoc($query)) {
$catName = $row['name'];
$catDes = $row['description'];
$catUrl = $row['url'];
echo "<tr class=''>";
echo "<td>$catName</td>";
echo "<td>$catDes</td>";
echo "<td>$catUrl</td>";
echo "</tr>";
}
?>
</table>
Now if the table was static, then I would just assign each alternating table row one of 2 styles in repeated order:
.whiteBackground { background-color: #fff; }
.grayBackground { background-color: #ccc; }
and that would be the end of that. However since the table rows are dynamically generated, how can I achieve this?
Or you could just use CSS:
table tr:nth-child(odd) {
background-color: #ccc;
}
<?php
$x++;
$class = ($x%2 == 0)? 'whiteBackground': 'grayBackground';
echo "<tr class='$class'>";
?>
It basically checks to see if $x is divisible evenly by 2. If it is, it is even.
P.S. if you haven't seen that style of if else query, it is called a ternary operator.
Set a variable to true/false or a number and then back again during each iteration. Or use the modulus operator such as $i%2==0 in a while loop where $i is a number and use this condition in a ternary statement or something that sets the class value of the <tr>
Easiest way to alternate row colors in PHP/HTML?
$i = 0;
while ( $row = mysql_fetch_assoc($result) ) {
echo '<tr class="' . ( ( $i %2 == 0 ) ? 'oneValue' : 'anotherValue' ) . '"><td>' . $row['something'] . '</td></tr>';
$i++;
}
<?
$color="1";
while ($line = mysql_fetch_array($result)) {
if($color==1){
echo '<tr bgcolor="">';
$color="2";
} else {
echo '<tr bgcolor="#dcdcdc">';
$color="1";
}
?><td align="left" width="40"><?= $line[name] ?></td>
<?
}
?>
This is my working code !
Here is my working part !
`
$i=1;
while($row = mysqli_fetch_array($result)) {
if($i%2==0)
{
echo '<tr bgcolor="#FFFF00">';
}
else
{
echo '<tr bgcolor="#99FFCC">';
}
$i++;
echo "<td>" . $row['blah'] . "</td>";
echo "<td>" . $row['blah_blah'] . "</td>";
echo "</tr>";
}
echo "</table>";
`
This is how I did it. I declared a css class called "even" with all the styling i wanted. Then looped through the scenario. Hope it helps!
<?php
include 'connect.php';
echo "<table id='hor-zebra'>";
$i = 0;
while($row = mysql_fetch_array($result))
{
if($i%2 == 0)
{
echo "<tr class='even'>";
echo "<td>" . $row['something'] ." ". $row['something'] . "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $row['something'] ." ". $row['something'] . "</td>";
echo "</tr>";
}
$i++;
}
echo "</table>";
mysql_close($con);
?>

Categories