Echoing PHP API Data In 3 Columns - php

I am getting website design ideas from a website API.
Currently my setup displays these designs in a table with only one column, I have over 40 items that need to be displayed in the table, but id prefer to display it in 3 columns. I have tried using i=0 ++ things etc but couldnt work out where to put it.
Here is my code so far:
<?php
$output = json_decode($output);
curl_close($ch);
echo '<table border="1">';
foreach($output as $template) {
echo '<tr>';
echo '<td>' . $template->template_name . '</td>';
echo '</tr>' . '<tr>';
echo '<td><img src="' . $template->thumbnail_url. '">' . '</td>';
echo '<tr>';
echo '<td><form method="GET" action=' . $_SERVER['PHP_SELF'] . '>';
echo '<input type="hidden" name="template_id" value="' . $template->template_id . '">';
echo '<input type="url" name="original_url" placeholder="Existing Site URL">'.'<br />';
echo '<input type="email" name="email" placeholder="Your e-mail" required>'.'<br />';
echo '<button type="submit">Choose Site</button>';
echo '</form></td>';
echo '</tr>';
}
echo '</table>';
}
?>
The output includes all of the website templates.
I am putting it into rows.The first row contains the name of the template, the second the picture of the website template and the third a form which I am working on.
My question is how can I nest the loop so that it does 3 columns, each row with 3 different designs?
Many thanks in advance

The common idea may be as the following, for example:
<?php
$array = [0, 1, 2, 3, 4, 5, 6, 7, 8];
$i = 1;
echo "<table>";
foreach ($array as $arr) {
if ($i % 3 === 0) {
echo "<tr class='class_3'>";
} else if ($i % 2 === 0) {
echo "<tr class='class_2'>";
} else {
echo "<tr class='class_1'>";
}
echo '<td>'.($i++).'</td>';
echo '<td>'.($i++).'</td>';
echo '<td>'.($i++).'</td>';
echo "</tr>";
//$i++; for 123 456
}
echo '</table>';
?>
Update: a more specific example:
<?php
$assoc_array = json_decode('
[
{"template_id": "1","template_name":"A", "preview_url":"url_1"},
{"template_id": "2","template_name":"B", "preview_url":"url_2"},
{"template_id": "3","template_name":"C", "preview_url":"url_3"},
{"template_id": "4","template_name":"D", "preview_url":"url_4"},
{"template_id": "5","template_name":"E", "preview_url":"url_5"},
{"template_id": "6","template_name":"F", "preview_url":"url_6"},
{"template_id": "7","template_name":"G", "preview_url":"url_7"},
{"template_id": "8","template_name":"H", "preview_url":"url_8"},
{"template_id": "9","template_name":"I", "preview_url":"url_9"},
{"template_id": "10","template_name":"J", "preview_url":"url_10"}
]
', true);
echo '<table>';
for($i = 0; $i < sizeof($assoc_array); $i++) {
if ($i % 3 === 0) {
echo "<tr class='class_3'>";
} else if ($i % 2 === 0) {
echo "<tr class='class_2'>";
} else {
echo "<tr class='class_1'>";
}
echo '<td>'.$assoc_array[$i]['template_name'].'</td>';
if(isset($assoc_array[++$i]['template_name'])) {
echo '<td>'.$assoc_array[$i]['template_name'].'</td>';
}
if(isset($assoc_array[++$i]['template_name'])) {
echo '<td>'.$assoc_array[$i]['template_name'].'</td>';
}
echo '</tr>';
}
echo '</table>';
?>
You'll get:
A B C
D E F
G H I
J

Thanks everyone who contributed. My answer is now finished.
The final code was:
<?php
$output = json_decode($output);
curl_close($ch);
$i = 0;
echo '<table border="1" width="80%">';
echo '<tr>';
foreach($output as $template) {
echo '<td><img src="' . $template->thumbnail_url. '" height="300" width="460">' .'<br />';
echo '<form method="GET" action=' . $_SERVER['PHP_SELF'] . '>';
echo '<input type="hidden" name="template_id" value="' . $template->template_id . '">';
echo '<input type="url" name="original_url" placeholder="Existing Site URL">'.'<br />';
echo '<input type="email" name="email" placeholder="Your e-mail" required>'.'<br />';
echo '<button type="submit">Choose Site</button>';
echo '</form></td>';
$i++;
if($i == 3) { // three items in a row. Edit this to get more or less items on a row
echo '</tr>'.'<tr>';
$i = 0;
}
}
echo '</tr>';
echo '</table>';
}
?>

Related

PHP: List multiple record grouped by date

I'm trying to list multiple record from MySQL Database using PHP but when grouping it returns only one record from database, below is my PHP code I'm using.
<?php
include "connection.php";
$query = "select * from lectureupload GROUP BY submittedOn";
$result=mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>'.$row['submittedOn'].'</h3>';
echo '</div>';
echo '<tr >';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['title'].'</td>';
echo '<td>'.$row['semester'].'</td>';
echo '<td>'.$row['teacherName'].'</td>';
echo '<td>'.$row['lectureLink'].'</td>';
echo '<td>'.$row['submittedOn'].'</td>';
echo '</tr>';
echo '</table>';
}
mysqli_close($conn);
?>
The current Result it gives me is somewhat like that,
My Database table is this.
Solution to this problem will be highly appreciated.
Regards.
You can't use GROUP BY clause to retrieve all records from that table. GROUP BY will always return one row per GROUP BY CONDITIONAL_COLUMN. I will suggest you below modification:
<?php
include "connection.php";
//ORDER BY submittedOn will make sure the records retrieved in ordered as per submittedOn.
$query = "select * from lectureupload ORDER BY submittedOn";
$result = mysqli_query($conn, $query);
$submitted_on_keys = array();
while ($row = mysqli_fetch_array($result)) {
if (!in_array($row['submittedOn'], $submitted_on_keys)) {
if (count($submitted_on_keys) > 0) {
//It means we have already created <table> which needs to be closed.
echo '</table>';
}
$submitted_on_keys[] = $row['submittedOn'];
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>' . $row['submittedOn'] . '</h3>';
echo '</div>';
}
echo '<tr >';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['title'] . '</td>';
echo '<td>' . $row['semester'] . '</td>';
echo '<td>' . $row['teacherName'] . '</td>';
echo '<td>' . $row['lectureLink'] . '</td>';
echo '<td>' . $row['submittedOn'] . '</td>';
echo '</tr>';
}
if (count($submitted_on_keys) > 0) {
//There is last <table> which needs to be closed.
echo '</table>';
}
mysqli_close($conn);
?>
You should group it in your PHP code rather than your SQL.
Something like this:
$lectures = [];
$result = mysqli_query($conn, "select * from lectureupload");
while ($row = mysqli_fetch_array($result)) {
$lectures[$row['submittedOn']][] = $row;
}
foreach ($lectures as $submittedOn => $rows) {
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>'.$submittedOn.'</h3>';
echo '</div>';
foreach ($rows as $row) {
echo '<tr >';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['title'].'</td>';
echo '<td>'.$row['semester'].'</td>';
echo '<td>'.$row['teacherName'].'</td>';
echo '<td>'.$row['lectureLink'].'</td>';
echo '<td>'.$row['submittedOn'].'</td>';
echo '</tr>';
}
echo '</table>';
}

How to increment a counter inside of a loop

I don't understand, I have a variable $i which is not incrementing in my loop.
while ($ligne = mysqli_fetch_array($resu2,MYSQL_ASSOC)) {
echo '<tr>';
for($i = 1; $nbjour >= $i; $i++)
{
while ($ligne2 = mysqli_fetch_array($resu3,MYSQL_ASSOC)) {
$element=$ligne2['jourIndisponibilite'];
echo "1er ".$element;
echo "2eme ".$ligne['idSPProfessionnel'];
echo ' ';
echo $i;
echo ' ';
if ($ligne2['jourIndisponibilite']==$i && $ligne['idSPPprofessionnel']==$ligne2['idSpProfessionnel']){
echo '<td height=25>';
echo 'X';
echo '</td>';
}
}
echo '<td height=20></td>';
}
echo '</tr>';
}
echo "</table>";
echo '</br>';
?>

How to add a pagination in a html table using PHP?

I have PHP snippet that displayed a result in the table row. I'm trying to a pagination at the end of the table with (1 2 3 4 ...10). The minimum row will be displayed every page is 15 only.
But my code is not working. Please help me
$results_per_page = 10;
$current = get_query_var('paged') ? get_query_var('paged') : 1;
$start = ($current - 1) * $rows_per_page;
$end = $start + $rows_per_page;
$end = (sizeof($rows) < $end) ? sizeof($rows) : $end;
if (isset($_POST['list_position']) && $_POST['list_position'] != 'Select by Position'){
$list_position= $_POST['list_position'];
$result_position= $wpdb->get_results($wpdb->prepare("SELECT DISTINCT id, submit_time, last_name, first_name, middle_name, mobile_number, email, location, position, message FROM tablename WHERE position= '" . $list_position . "' ORDER BY position ASC", OBJECT));
echo '<table>';
echo '<tr>';
$dir="";
$file="";
$optionId = 0;
echo '<th>Submit Time</th>';
echo '<th>Last Name</th>';
echo '<th>First Name</th>';
echo '<th>Middle Name</th>';
echo '<th>Mobile Number</th>';
echo '<th>Email</th>';
echo '<th>Position</th>';
echo '<th>Message</th>';
echo '<th>Resume</th>';
echo '<th>Processed?</th>';
foreach ($result_position as $record_s){
$optionId++;
//$optionId = $record_s['id'];
for ($i=$start;$i < $end ;++$i ){
echo '<tr>';
echo '<td id="submit_time">' . $record_s->submit_time . '</td>';
echo '<td id="last_name">' . $record_s->last_name . '</td>';
echo '<td id="first_name">' . $record_s->first_name . '</td>';
echo '<td id="middle_name">' . $record_s->middle_name . '</td>';
echo '<td id="mobile_number">' . $record_s->mobile_number . '</td>';
echo '<td id="email">' . $record_s->email . '</td>';
echo '<td id="position">' . $record_s->position . '</td>';
echo '<td id="message">' . $record_s->message . '</td>';
echo '<td id="resumeFile'.$id.'">Download Resume</td>';
echo '<td id="radioOption><label for="Yes">Yes</label>
<input type="radio" id="processedOptionYes'.$optionId.'" name="processedOption" value="Yes" onclick="proccessedCheck('.$optionId.',\'Yes\')"/>
<label for="No">No</label>
<input type="radio" id="processedOptionNo'.$optionId.'" name="processedOption" value="No" onclick="proccessedCheck('.$optionId.',\'No\')"/></td>';
echo '</tr>';
}
}
echo '</table>';
}
jQuery pagination one another option if you want to try? https://datatables.net/examples/basic_init/zero_configuration.html Here is datatables link you can easily implements in php.

How to get array index from foreach loop [duplicate]

This question already has answers here:
How to find the foreach index?
(14 answers)
Closed 7 years ago.
how to get array index from foreach looping, i use session to print cart data like this, i need to print the index like 1, 2, 3, 4 before the code column in the table
<?php
if(isset($_SESSION["products"]))
{
$total = 0;
echo '<tbody>';
foreach ($_SESSION["products"] as $cart_itm)
{
echo '<tr>';
echo '<td>?</td>';
echo '<td>'.$cart_itm["code"].'</td>';
echo '<td>'.$cart_itm["name"].'</td>';
echo '<td>'.$cart_itm["qty"].'</td>';
echo '<td><input type="text" name="product_qty_desired" class="spinner" value="1" size="3" /></td>';
echo '<td>'.$currency.$cart_itm["price"].'</td>';
echo '<td class="subtotal">Subtotal : </td>';
echo '<td><span class="remove-itm">×</span></td>';
echo '</tr>';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
}
echo '<tr>';
echo '<td></td>';
echo '<td></td>';
echo '<td>Total QTY :</td>';
echo '<td></td>';
echo '<td>?</td>';
echo '<td>Total</td>';
echo '<td>'.$currency.$total.'</td>';
echo '<td></td>';
echo '</tr>';
echo '</tbody>';
}else{
echo 'Your Cart is empty';
}
?>
http://i62.tinypic.com/2qaifrt.png
Thanks
Warmly
foreach ($_SESSION["products"] as $index => $cart_itm)
{
echo '<tr>';
echo '<td>' . $index . '</td>';
echo '<td>'.$cart_itm["code"].'</td>';
echo '<td>'.$cart_itm["name"].'</td>';
echo '<td>'.$cart_itm["qty"].'</td>';
echo '<td><input type="text" name="product_qty_desired" class="spinner" value="1" size="3" /></td>';
echo '<td>'.$currency.$cart_itm["price"].'</td>';
echo '<td class="subtotal">Subtotal : </td>';
echo '<td><span class="remove-itm">×</span></td>';
echo '</tr>';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
}
With the arrow operator, you can select the index in a foreach loop.

PHP display associative array in HTML table

Here is my associative array:
$req_data1[]=array(
'depart1'=>$_REQUEST['to'],
'd_time1'=>$d_time5,
'stop'=>"",
'leave_stop'=>"",
'arrival1'=>$_REQUEST['from'],
'a_time1'=>$end_time5,
'price1'=>intval($final_price),
'air_line'=>"xxxxx");
Here is my sorting algorithm:
foreach ($req_data as $key => $row) {
$depart[$key] = $row['depart'];
$d_time[$key] = $row['d_time'];
$stop[$key] = $row['stop'];
$leave_stop[$key] = $row['leave_stop'];
$arrival[$key] = $row['arrival'];
$a_time[$key] = $row['a_time'];
$price[$key] = $row['price'];
}
array_multisort($price,SORT_ASC, $req_data);
I am displaying the data after sorting:
foreach($req_data as $key=>$row) {
echo "</br>";
foreach($row as $key2=>$row2){
echo $row2;
}
}
My problem now is that I want to put that array into an HTML table but dont know how. This is my code which I tried so far but its not working:
$cols = 5;
echo "<table border=\"5\" cellpadding=\"10\">";
for ($r=0; $r < count($row2); $r++) {
echo "<tr>";
for ($c=0; $c<$cols; $c++) {
?> <td> <?php $input[$r+$c] ?> </td> <?php
}
echo "</tr>";
$r += $c;
}
echo "</table>";
?>
Could any one tell me what is wrong with my code, or how I can display this sorted data into a table? Thanks.
Why not just modify the loop you already use to display the data?
echo "<table>";
foreach($req_data as $key=>$row) {
echo "<tr>";
foreach($row as $key2=>$row2){
echo "<td>" . $row2 . "</td>";
}
echo "</tr>";
}
echo "</table>";
$toOutput = '<table>';
$showHeader = true;
$memberData = $reportObj->getMemberData();
while($row = mysql_fetch_assoc($memberData))
{
$toOutput .= '<tr>';
//Outputs a header if nessicary
if($showHeader)
{
$keys = array_keys($row);
for($i=0;$i<count($keys);$i++)
{
$toOutput .= '<td>' . $keys[$i] . '</td>';
}
$toOutput .= '</tr><tr>';
$showHeader = false;
}
//Outputs the row
$values = array_values($row);
for($i=0;$i<count($values);$i++)
{
$toOutput .= '<td>' . $values[$i] . '</td>';
}
$toOutput .= '</tr>';
}
$toOutput .= '</table>';
echo 'Test page';
echo $toOutput;
Sorry for the necro, but I was actually looking to see if there was a build-in function for this as I was writing this.
function DumpTable($array_assoc) {
if (is_array($array_assoc)) {
echo '<table class="table">';
echo '<thead>';
echo '<tr>';
list($table_title) = $array_assoc;
foreach ($table_title as $key => &$value):
echo '<th>' . $key . '</th>';
endforeach;
echo '</tr>';
echo '</thead>';
foreach ($array_assoc as &$master):
echo '<tr>';
foreach ($master as &$slave):
echo '<td>' . $slave . '</td>';
endforeach;
echo '</tr>';
endforeach;
echo '</table>';
return;
}
}
echo "<table border=\"5\" cellpadding=\"10\">";
for ($r=0; $r < count($row2); $r++) {
echo "<tr>";
for ($c=0; $c<$cols; $c++) { ?>
<td> <?php $input[$r+$c] ?> </td>
<?php }
echo "</tr>";
$r += $c;
}
echo "</table>";?>
Try something like this
echo "<table>";
for($r=0;$r<count($row2);$r++){
echo "<tr>";
for($c=0;$c<$cols;$c++){
echo "<td>".[VARIABLE YOU WANT TO PRINT]."</td>";
}
echo "</tr>";
}
echo "</table>";
you can try the following:
echo "The associative array<br>";
$computer=array("brand"=>"hp", "price"=>"800", "cpu"=>"core i7");
$keys=array_keys($computer);
echo "<table><tr>";
foreach($keys as $row){
echo "<th style=\"border: solid 2px green\">".$row."</th>";
}echo "</tr><tr>";
foreach($computer as $col){
echo "<td style=\"border: solid 2px blue\">".$col."</td>";
}echo "</tr></table>";

Categories