How to create PHP two column table with values from the database?
well, if there's no relation and the table is used only for layout:
echo '<div class="container">';
while($res = mysql_fetch_array($q)){
echo '<div class="item">'. $res['id'] . $res['title'] . '</div>';
}
echo '</div>';
and in css:
.container { width: 400px; float: left; }
.container .item { width: 50%; float: left; height: someFixedHeight; }
// or 200px
anyways, it's my preference to use tables only for displaying actual tables and avoid using them for layout. you can do anything you want with div's (or in this case you can also use ul and li. Of course it's not a must but normally it requires less HTML and for SEO the html-content ratio is something to consider. if you don't want fixed heights you can wrap each row as with the td/tr examples above.
how to display data in two column with php
The answer also depends on the way you'd like to show the info.
If we assume you have the following six artist_id's in your DB
1,2,3,4,5,6
in which way would you like to show them? This way
1 2
3 4
5 6
or this way?
1 4
2 5
3 6
Update: as you say you'd like to show them in the first way, the solution is pretty simple. Every pair iteration (0, 2, 4...) you should open the row and every odd iteration (1, 3, 5...) you should close it , so you'd have
<tr><td>0</td><td>1</td></tr><tr><td>2</td><td>3</td></tr>...
The code would be
<?php
$query = "SELECT * FROM `release` WHERE artist_id = '$rcode' AND label_id = '$id'";
$result = mysql_query($query);
$tr_no = mysql_num_rows($result);
$ii = 0; // Iterator
while ($info = mysql_fetch_array($result)) {
?>
<div>
<table style="width: 100%">
<?php if ($ii%2 == 0) { // If pair, we open tr?>
<tr>
<?php } ?>
<td ><img src="../artwork/<?php echo $info['label_id']; ?>/<?php echo $info['ID']; ?>.jpg" width="100" height="100" /></td>
<td valign="top">
<table style="width: 100%">
<tr>
<td style="width: 45px; height: 20px;" class="style5">
</td>
<td style="width: 180px"><?php echo $info['code']; ?></td>
</tr>
<tr>
<td style="width: 45px; height: 20px;" class="style5">
</td>
<td style="width: 180px"><?php echo $info['name']; ?></td>
</tr>
<tr>
<td style="width: 45px; height: 20px;" class="style5">
</td>
<td style="width: 180px"><?php echo trh($info['date']); ?></td>
</tr>
<tr>
<td style="width: 45px; height: 20px;" class="style5">
</td>
<td style="width: 180px">
</td>
</tr>
</table>
</td>
<?php if ($ii%2 == 1) { // If odd, we close tr?>
</tr>
<?php } ?>
</table>
</div>
<?php $ii++; // We increment the iterator }
?>
how to display data in two column with php with header
This is representation problem, so it can be solved it two ways.
1. You can make this columns using modern HTML:
<section style="-webkit-column-count:2; -webkit-column-gap:15;">
<div class='columns'>
<table>
<tr>
<th>no</th>
<th>name</th>
<th>code</th>
<th>grade</th>
</tr>
<?php
$result = mysql_query("select * from grade ");
$no = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>';
echo "<td>{$row['no']}</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['code']}</td>";
echo "<td>{$row['grade']}</td>";
echo '</tr>';
$no++;
}
?>
</table>
</div>
</section>
2. If you want to stick to plain tables, you have to do some calculations:
First, you need to know full amount of grades you gonna show. You can get it with SQL query(more efficient way), I'll do it by preloading all data to array. HTML footer and header are omitted for brevity.
<?php
$data = array();
$result = mysql_query('SELECT * FROM grade;');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$data[] = $row;
}
$rows = (int) ceil(count($data)/2);
for ($i=0; $i < $rows; $i++) {
$no = $i + 1;
echo '<tr>';
echo "<td>{$no}</td>";
echo "<td>{$data[$i]['name']}</td>";
echo "<td>{$data[$i]['code']}</td>";
echo "<td>{$data[$i]['grade']}</td>";
if (array_key_exists($i + $rows, $data)) {
$no = $i + $rows + 1;
echo "<td>{$no}</td>";
echo "<td>{$data[$i + $rows]['name']}</td>";
echo "<td>{$data[$i + $rows]['code']}</td>";
echo "<td>{$data[$i + $rows]['grade']}</td>";
} else {
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
}
echo "</tr>\n";
}
How do I get the data in two columns inside a database table using php
It sounds like it should just be something like
print "<label><input type='radio' name='course' value='".$row["course_id"]."'>".$row["department"]."</label><br/>";
Assuming "department" is the name of your database column then this would print the department name next to each radio button.
How to relate two column in PHP MYSQL
You can use conditions in your query in order to select the right column depending on another column's value.
SELECT requestid,
(CASE WHEN itemrequest1 = '$itemrequest' THEN quantity1
ELSE (
CASE WHEN itemrequest2 = '$itemrequest' THEN quantity2
ELSE (
CASE WHEN itemrequest3 = '$itemrequest' THEN quantity3
END)
END)
END)
FROM tbl_request
You should have a close look to your database structure though, if you have to do things like this, it might be better to improve your structure and have better tables/fields.
fetch 2 columns from table in 2 array php mysql
Found a way to do it. I translated the code to English so others can have it. If there is any variable name wrong, fix it using the comments and enjoy!
$sql = "SELECT parquet, defendant, action, modusOperandi, victim, crime FROM crime_elements";
//runs '$sql' query to get database data
$result = mysqli_query($con,$sql);
$collNumber = mysqli_num_fields($result);
echo "ColL Number :" . $collNumber;
// Creates $data Array[]
$data = array();
while($row = mysqli_fetch_array($result)){
//inserts received data into "$data" and makes available by array[]index
$data[] = $row;
}
for($i=0;$i<=$collNumber -1;$i++){
// Gets the column name from the query()
$fieldName = mysqli_fetch_field_direct($result, $i)->name;
echo "</br><b>Coll Name:</b> " . $fieldName . "<br /><br />";
${$fieldName} = array_column($data, $i);
print_r(${$fieldName});
echo "<br /><b>Specified item choice[]:</b> ". ${$fieldName}[2];
echo "<hr />";
}
Display result from database in two columns
A good idea would be storing your data into a simple array and then display them in a 2-columned table like this:
$con = mysql_connect('$myhost', '$myusername', '$mypassword') or die('Error: ' . mysql_error());
mysql_select_db("mydatabase", $con);
mysql_query("SET NAMES 'utf8'", $con);
$q = "Your MySQL query goes here...";
$query = mysql_query($q) or die("Error: " . mysql_error());
$rows = array();
$i=0;
// Put results in an array
while($r = mysql_fetch_assoc($query)) {
$rows[] = $r;
$i++;
}
//display results in a table of 2 columns
echo "<table>";
for ($j=0; $j<$i; $j=$j+2)
{
echo "<tr>";
echo "<td>".$row[$j]."</td><td>".$row[$j+1]."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
Combine multiple SQL columns into a single column of an HTML table using PHP
No need to have two loops. It can be done in single loop. Separate the col1 and col2 values in different variables. and at the end print it.
<?php
$con = new mysqli('domain', 'username', 'password', 'database');
$sql = "select * from table1";
$sql_query = mysqli_query($con, $sql);
$str = "<table>";
$str_col2 = "";
while ($row = mysqli_fetch_array($sql_query)) {
$col1 = $row['col1'];
$col2 = $row['col2'];
$str .= "<tr><td> $col1 </td></tr>";
$str_col2 .= "<tr><td> $col2 </td></tr>";
}
echo $str . $str_col2 . "</table>";
?>
Related Topics
Class Firebase\Jwt\Jwt Not Found
Crypt for Password Hashing. Blowfish Produces Weird Output
Is Using Superglobals Directly Good or Bad in PHP
Why Doesn't PHP Dom Include Slash on Self Closing Tags
PHP Mail Function: Legitimate Mails Marked as Spam by Gmail and Hotmail
How to Handle Newlines in JavaScript? (From PHP)
Get Order Increment Id in Magento
Check How Many Times Specific Value in Array PHP
Phpmailer Error: Smtp -> Error: Failed to Connect to Server
Mysql_Data_Seek Pdo Equivalent
Can You Pass by Reference While Using the Ternary Operator
PHP - How to Get Shell Errors Echoed Out to Screen
Phpmailer Send Gmail Smtp Timeout
Pass Value from HTML Form to PHP Without Submitting the Form
PHP Headers Already Sent Error
Change Pay Button on Checkout Based on Woocommerce Chosen Payment Method