I'm about month and a half into PHP. I tried to make function for fetching results from find_all_subjects():
function find_all_subjects() {
global $dbconnect;
$q = "SELECT * FROM subjects ORDER BY id ASC";
$subject_set = mysqli_query($dbconnect, $q);
confirm_query($subject_set);
return $subject_set;
}
and then making a new function which will loop the result in table rows. The problem is it loops just one result... when I did it with <ul> and <li> it worked fine but it doesn't seem to work when doing it with the table. My table core tags are in another document. So it's not that...
function navigacija() {
$s = find_all_subjects();
while($subjects = mysqli_fetch_assoc($s)) {
$out = "<tr>";
// Ime Teme
$out .= "<td width='25%' height='40'> <a href=\"admin_content.php?subject=" . urlencode($subjects['id']) . "\">";
$out .= $subjects['menu_name'];
$out .= "</a></td>";
// Vidljiva
if($subjects['visible'] == 1) {
$subjects['visible'] = 'DA';
} else {
$subjects['visible'] = 'NE';
}
$out .= "<td width='25%' height='40'><p align=\"center\">DA / NE";
$out .= "</p></td>";
// Broj Strana
$out .= "<td width='25%' height='40'>";
$pages_set = find_sub_from_pages($subjects['id']);
while($pages = mysqli_fetch_assoc($pages_set)) {
$out .= "" . $pages['menu_name'] . "";
$out .= "</td>";
}
// Vidljiva
$out .= "<td align=\"center\" width='25%' height='40'>";
$out .= "<img width=\"17px\" height=\"17px\" src=\"st/img/ic-arup.png\">
</img> <img width=\"17px\" height=\"17px\" src=\"st/img/ic-ardown.png\"></img> ";
$out .= "</td>";
$out .= "</tr>";
}
return $out;
}
the problem is that you reset
$out = "<tr>";
every time in the loop.
change this line to
$out .= "<tr>";
and only put declaration out of the loop
$out = "";
Related
How do I make a NxM table where each cell has two rows and its second row has two columns, but by using rowspan or colspan? I want to make something like this:
Here is my attempt.
Artikal.php:
public function get_html()
{
$html = "";
$html .= "<td>";
if (isset($this->sifra) && isset($this->naziv) && isset($this->cena) && isset($this->kolicina)) {
if ($this->kolicina > 0) {
$html .= "<img src=\"images/" . str_replace(" ", "-", $this->naziv) . jpg\" width=\"100\">";
$html .= "<br>";
$html .= "<span class=\"sifra\">{$this->sifra}</span>";
$html .= "<span class=\"cena\">{$this->cena} DIN</span>";
} else {
$html .= "<img src=\"images/none.png\" width=\"100\">";
$html .= "<br>";
$html .= "<span class=\"sifra\">{$this->sifra}</span>";
$html .= "<span class=\"cena\">{$this->cena} DIN</span>";
}
} else {
$html .= "<img src=\"images/none.jpg\" width=\"100\">";
$html .= "<br>";
$html .= "<span class=\"sifra\">X</span>";
$html .= "<span class=\"cena\">X</span>";
}
$html .= "</td>";
return $html;
}
index.php
if (isset($artikli)) {
for ($i = 0; $i < 6; $i++) {
echo "<tr>";
for ($j = 0; $j < 9; $j++)
echo $artikli[$i * 9 + $j]->get_html();
echo "</tr>";
}
}
Hi I am trying to read a nested JSON array data into a table and as a list not just in a line.
Look, at the way it comes out:
Link to JSON: JSON
L: demo
P: ocfB6XzF73
Documentation: URL-DOC
JSON code I am trying to reach:
"EquipmentList": [ "ABSBrakes", "Alarm", "AlloyRims", "AntiSpin", "AutomaticGear", "RemoteCentralLocking", "PoweredWindows", "PoweredMirrorsHeated", "CruiseControl", "InfoCenter", "AutomaticClimateControl", "TripComputer", "Navigation", "GearShiftStearingWheel", "ServiceOK", "Immobilizer", "SeatHeater", "XenonLight" ]
My code so far i very simple as I struggle to list the data to a list i.e. LI or a TABLE :
<?php
$url = 'https://gw.bilinfo.net/listingapi/api/export';
// provide your username and password here
$auth = base64_encode("demo:ocfB6XzF73");
// create HTTP context with basic auth
$context = stream_context_create([
'http' => ['header' => "Authorization: Basic $auth"]
]);
// query for data
$data = file_get_contents($url, false, $context);
// $escaped = json_encode($data);
$escaped = json_decode($data); //, JSON_FORCE_OBJECT
/*Initializing temp variable to design table dynamically*/
$temp = "<table>";
/*Defining table Column headers depending upon JSON records*/
$temp .= "<tr>";
$temp .= "<th>Bil</th>";
$temp .= "<th>Model</th>";
$temp .= "<th>Motor</th>";
$temp .= "<th>Drivmiddel</th>";
$temp .= "<th>Udstyr</th>";
$temp .= "<th>Billeder</th>";
$temp .= "</tr>";
/*Dynamically generating rows & columns*/
foreach ($escaped->Vehicles as $vehicle) {
$temp .= "<tr>";
$temp .= "<td>" . $vehicle->Make . "</td>";
$temp .= "<td>" . $vehicle->Model . "</td>";
$temp .= "<td>" . $vehicle->Motor . "</td>";
$temp .= "<td>" . $vehicle->Propellant . "</td>";
foreach($escaped->Vehicles[0]->EquipmentList as $EquipmentItem){
$temp .= "<td>" . $EquipmentItem . "</td>";
}
for ($p = 0; $p < $vehicle->PictureCount; $p++) {
$temp .= "<td><img src='" . $vehicle->Pictures[0] . "'></td>";
}
}
$temp .= "</tr>";
/*End tag of table*/
$temp .= "</table>";
/*Printing temp variable which holds table*/
echo $temp;
echo $data;
?>
With the help of Anand Pandey
<?php
$url = 'https://gw.bilinfo.net/listingapi/api/export';
// provide your username and password here
$auth = base64_encode("demo:ocfB6XzF73");
// create HTTP context with basic auth
$context = stream_context_create([
'http' => ['header' => "Authorization: Basic $auth"]
]);
// query for data
$data = file_get_contents($url, false, $context);
// $escaped = json_encode($data);
$escaped = json_decode($data); //, JSON_FORCE_OBJECT
/*Initializing temp variable to design table dynamically*/
$temp = "<table class='car-list'>";
/*Defining table Column headers depending upon JSON records*/
$temp .= "<tr>";
$temp .= "<th class='th-style'>Bil</th>";
$temp .= "<th class='th-style'>Model</th>";
$temp .= "<th class='th-style'>Motor</th>";
$temp .= "<th class='th-style'>Drivmiddel</th>";
$temp .= "<th class='th-style'>Udstyr</th>";
$temp .= "<th class='th-style'>Billeder</th>";
$temp .= "</tr>";
/*Dynamically generating rows & columns*/
foreach ($escaped->Vehicles as $vehicle) {
$temp .= "<tr>";
$temp .= "<td class='td-style'>" . $vehicle->Make . "</td>";
$temp .= "<td class='td-style'>" . $vehicle->Model . "</td>";
$temp .= "<td class='td-style'>" . $vehicle->Motor . "</td>";
$temp .= "<td class='td-style'>" . $vehicle->Propellant . "</td>";
$temp .= "<td class='td-style'>";
foreach ($escaped->Vehicles[0]->EquipmentList as $EquipmentItem) {
$temp .= "<table>";
$temp .= "<tr>";
$temp .= "<td class='equipmentlist'>" . $EquipmentItem . "</td>";
$temp .= "</tr>";
$temp .= "</table>";
}
$temp .= "</td>";
$temp .= "<td class='td-style'>";
for ($p = 0; $p < $vehicle->PictureCount; $p++) {
$temp .= "<table>";
$temp .= "<tr>";
$temp .= "<td class='td-style'>";
$temp .= "<img class='cc-images' src='" . $vehicle->Pictures[$p] . "'>";
$temp .= "</td>";
$temp .= "</tr>";
$temp .= "</table>";
}
$temp .= "</td>";
}
$temp .= "</tr>";
/*End tag of table*/
$temp .= "</table>";
/*Printing temp variable which holds table*/
echo $temp;
//echo $data;
?>
So I'm exploring the wonderful world of PHP and I'm still creating very dirty, poorly build code but I'm trying to get better! So my question is as follows:
Is there a way to automatically calculate the columns in the result set and spit out a pretty HTML table regardless of query used?
Here the current code:
<?php
include '../includes/connect.php';
include '../includes/queries.php';
$stid = oci_parse($conn, $export);
oci_execute($stid);
echo "<table class='pure-table pure-table-striped' style='font-size:11px;'><thead><tr><th>Name</th><th>File Name</th><th>Export Date</th></tr></thead>";
while (oci_fetch($stid)) {
echo "<tr><td>" . oci_result($stid, 'DISPLAY_NAME') . "</td>";
echo "<td>" . oci_result($stid, 'LAST_EXPORT_FILE') . "</td>";
echo "<td>" . oci_result($stid, 'LAST_EXPORT_DATE') . "</td></tr>";
}
echo "</table>\n";
oci_free_statement($stid);
oci_close($conn);
I'd like to use the same table every time, but just have it auto detect column header names and number of columns and return it in a table.
Is it possible or does it make sense?
I do exactly that using PDO.
$out = '';
$q = $conn->prepare($SQL);
if ($q->execute()) {
$rows = $q->fetchAll();
if (!empty($rows)) {
//We have something
$out .= "<table class='pure-table pure-table-striped' style='font-size:11px;'>";
$first = true;
//iterate on rows
foreach($rows as $row) {
//Clean out all the numeric keys - stops duplicate values
foreach ($row as $key => $value) {
if (is_int($key)) {
unset($row[$key]);
}
}
//header
if ($first) {
//write header
$out .= '<thead><tr>';
foreach ($row as $key => $value) {
$out .= '<th>' . $key . '</th>';
}
$out .= '</tr></thead>';
$first = false;
}
//write line
$out .= '<tr>';
foreach($row as $key => $value) {
$out .= '<td>' . $value . '</td>';
}
$out .= '</tr>';
}
$out .= '</table>';
}
}
echo ($out);
function find_image_by_id() {
global $connection;
$query = "SELECT * ";
$query .= "FROM images ";
$query .= "WHERE page_id={$_GET["page"]}";
$image_set = mysqli_query($connection, $query);
confirm_query($image_set);
return $image_set;
}
function display_image_by_id(){
$current_image = find_image_by_id();
while($image=mysqli_fetch_assoc($current_image)){
$output = "<div class=\"images\">";
$output .= "<img src=\"images/";
$output .= $image["ilink"];
$output .= "\" width=\"72\" height=\"72\" />";
$output .= $image["phone_name"];
$output .= "</div><br />";
}
mysqli_free_result($current_image);
return $output;
}
This is the code I'm using to show the images stored as links in mysql
and the images are in a folder. But what happens after this code is executed only the second
value is displayed. I want both value/ images to be displayed.
Try something like that-
All you need to do is just initialize this variable outside the loop.
$output =''; //initialize before
SO your function look like this -
function display_image_by_id(){
$current_image = find_image_by_id();
$output =''; //initialize before
while($image=mysqli_fetch_assoc($current_image)){
$output .= "<div class=\"images\">";
$output .= "<img src=\"images/";
$output .= $image["ilink"];
$output .= "\" width=\"72\" height=\"72\" />";
$output .= $image["phone_name"];
$output .= "</div><br />";
}
mysqli_free_result($current_image);
return $output;
}
I am trying to display a table in while loop with 3 rows and 3 td for each row. But I am confusing how to archived thit.
This is my code so far:
while($row = mysqli_fetch_array($result)){
$testimonial = $row['testimonial'];
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
$testimonial = nl2br($testimonial);
//Create new testimonial output
$output = "<table>\n";
$output .= " <tr>\n";
$output .= " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
$output .= " </tr>\n";
$output .= "</table>\n";
echo $output;
}
The above code given me a table with multiple rows and 1 td for each row. But it is not my expecting result.
Can anybody tell me how can I display such a table in my While loop?
The current code creates a new table for every single entry. What you want is to move the <table> and </table> pieces outside of the loop, then you want a counter to keep track of how many cells you've handled. If it's even, start a new <tr>. Otherwise, end the current </tr>. Make sure you check at the end to see if you've finished a </tr>, and optionally add an empty <td></td> if needed.
this aught to do it
<?php
echo "<table>\n";
$cells = $total = 0;
$total_cells = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result))
{
$testimonial = nl2br($row['testimonial']);
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
if ($cells === 0)
echo "<tr>\n";
//Create new testimonial output
$output = " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
echo $output;
$cells++;
$total++;
if ($cells === 3 || $total === $total_cells)
{
echo "</tr>\n";
$cells = 0;
}
}
echo "</table>\n";
?>
Try this
$i = 0;
echo "<table>\n<tr>";
while($row = mysqli_fetch_array($result)){
$testimonial = $row['testimonial'];
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
$testimonial = nl2br($testimonial);
//Create new testimonial output
$output .= " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
echo $output;
if( $i %2 == 1 )
echo "</tr><tr>\n";
$i++;
}
echo "</tr>\n</table>\n";
EDIT
In general, for displaying n cells per row, change the if statement to
if( $i % n == (n - 1) )