Table problems in PHP - php

This is my current code (and image showing output). I want the table to be aligned and the Names of the values keep repeating. How can I achieve this?
<?php
$country['ph'] = array('Philippines', 'Manila', '+8');
$country['ru'] = array('Russia', 'Moscow', '-4');
$ctr = 1;
foreach ($country as $key => $value) {
echo '<table border = "1" >';
echo '<td>No';
echo '<td>Flag';
echo '<td>Code';
echo '<td>Name';
echo '<td>Capital';
echo '<td>Time Zone';
echo '<tr>';
echo '<td>', $ctr++, '</td>';
echo "<td><img src = \"/csnclass/img/flags/$key.png\"/></td>";
echo "<td>$key</td>";
echo '<td>',$value[0], '</td>';
echo '<td>',$value[1], '</td>';
echo '<td>',$value[2], '</td>';
echo '</tr>';
echo '</table>';
}
?>

Presently, your foreach is outputting a <table> for each item it iterates through - so the table cells in these tables won't be lined up, as cells in different tables have no relation to each other. Similarly, you're outputting the header of each table column for every single item as well.
A quick way to fix this is to take some of the echo calls outside of the foreach loop (mainly relating to the table and headers):
// Start table and print out headers
echo '<table border = "1" >';
echo '<tr>';
echo '<td>No';
echo '<td>Flag';
echo '<td>Code';
echo '<td>Name';
echo '<td>Capital';
echo '<td>Time Zone';
echo '</tr>';
// Print data for each country
foreach ($country as $key => $value) {
echo '<tr>';
echo '<td>', $ctr++, '</td>';
echo "<td><img src = \"/csnclass/img/flags/$key.png\"/></td>";
echo "<td>$key</td>";
echo '<td>',$value[0], '</td>';
echo '<td>',$value[1], '</td>';
echo '<td>',$value[2], '</td>';
echo '</tr>';
}
// End table
echo '</table>';
Hope this helps! Let me know if you have any questions.

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

ID'd link on click

I'm creating a contact based system, i have a contact list and want it to go to a full contact page when i click on a button, but the variable is being detected as a function?
<div id="po2" style="Margin:10% ">
<h1>Contacts</h1>
<?php
$sql = "SELECT * FROM `contacts`";
$query = mysqli_query($conn, $sql);
echo '<table class="data-table">';
echo'<thead>';
echo'<tr>';
echo '<th>Forename</th>';
echo '<th>Surname</th>';
echo '<th>Other</th>';
echo'</tr>';
echo '</thead>';
echo '<tbody>';
while ($row = mysqli_fetch_array($query))
{
echo '<tr>';
echo '<td>'.$row['Forename'].'</td>';
echo '<td>'.$row['Surname'].'</td>';
echo $var==$row['Forename']("<td><a href='View.php?ID= " . urlencode($var) ."'>
<button type='button'>link</button>
</a></td>");
echo '</tr>';
}
echo'</tbody>';
echo '</table>';
?>
</div>
You are using a comparison of $var and the $row. Try setting $var to the $row each loop iteration.
echo '<thead>';
echo '<tr>';
echo '<th>Forename</th>';
echo '<th>Surname</th>';
echo '<th>Other</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while ($row = mysqli_fetch_array($query))
{
$var = $row['Forename'];
echo '<tr>';
echo '<td>' . $var . '</td>';
echo '<td>' . $row['Surname'] . '</td>';
echo "<td><a href='View.php?ID=" . urlencode($var) . "'><button type='button'>link</button></a></td>";
echo '</tr>';
}
echo '</tbody>';

change array output with changing value

So I have a table with a value $i for a column that simply counts/labels each row (goes from 1 to 12).
I have an array that I would like to add, into another table column. What I am trying to do, is have the array change the value it is outputting each time the $i value changes.
This is sort of what it looks like as for coding:
<?php
$i=1;
$myarray= array('one', 'two', 'three', 'etc...')
?>
<center>
<?php
echo'<table width="100%">';
echo '<tr>';
echo '<th></th>';
echo '<th>Country</th>';
echo '<th>Greeting</th>';
echo '<th>Translation</th>';
echo '<th>Language</th>';
echo '</tr>';
$number_of_rows = 11;
for ($row = 0; $row <= $number_of_rows; $row++) {
echo '<tr>';
echo '<td >'. $i++ . '</td>';
echo '<td >'. “Blue” . '</td>';
echo '<td >'. “Red” . '</td>';
echo '<td >'. “Green” . '</td>';
echo '<td >'. “Black” . '</td>';
echo '</tr>';
}
?>
</center>
I'm quite new to php, so it's a bit confusing to me. Sorry for any mistakes.
This is where I'm a bit stuck. I want the row that says "Green" to be the row that I am going to implement my array. Each row it will say something different, as the $i value is different for each row. However, I'm not quite sure how to implement this with the current set up that I have going? Is there a way I can do it? Or do I have to take a different route?
Thanks.

Enumerate table rows in html

I have this code which prints a table taken from a search query and I want to enumerate them using PHP. I'm trying a for loop but unsuccessfully. Can anyone please help me?
<table id="students">
<?php
$iterator = new CDataProviderIterator($dataProvider);
//echo count($iterator);
echo '<thead>';
echo '<tr>';
echo '<th>'.Yii::t('default', 'Last Name').'</th>';
echo '<th>'.Yii::t('default', 'First Name').'</th>';
echo '<th>'.Yii::t('default', 'Description').'</th>';
echo '<th>'.Yii::t('default', 'Debit').'</th>';
echo '<th>'.Yii::t('default', 'Credit').'</th>';
echo '<th>'.Yii::t('default', 'Date').'</th>';
echo '<th>'.Yii::t('default', 'Trans number').'</th>';
echo '</tr>' ;
echo '</thead>';
echo '<tfoot>';
echo '<tr>';
echo '<td>'.Yii::t('default', 'Page Total').'</td>';
echo '<td>'."".'</td>';
echo '<td>'."".'</td>';
echo '<td>'."".'</td>';
echo '<td>'."".'</td>';
echo '<td>'."".'</td>';
echo '<td>'."".'</td>';
echo '</tr>';
echo '</tfoot>';
$sumdebit=0;
$sumcredit=0;
foreach($iterator as $rec) {
$sumdebit+=$rec['debit'];
$sumcredit+=$rec['credit'];
echo '<tbody>';
echo '<tr>';
$count=0;
for ($i=0;$i<=40;$i++){
$count+=$i;
echo '<td>'.$count; '</td>';
echo '<td>'.$rec['student']['lastname']; '</td>';
echo '<td>'.$rec['student']['firstname'];'</td>';
echo '<td>'.$rec['transtype']['description']; '</td>';
echo '<td align="right">'.$rec['debit']; '</td>';
echo '<td align="right">'.$rec['credit']; '</td>';
echo '<td>'.$rec['transdate']; '</td>';
echo '<td>'.$rec['transnumber']; '</td>';
}
echo '</tr>';
echo '</tbody>';
}
echo '<tr>';
echo '<td>'.Yii::t('default', 'Total').'</td>';
echo '<td>'."".'</td>';
echo '<td>'."".'</td>';
echo '<td align="right">'.$sumdebit;'</td>';
echo '<td align="right">'.$sumcredit;'</td>';
echo '<td>'."".'</td>';
echo '<td>'."".'</td>';
echo '</tr>';
?>
</table>
My problem is that I want to enumerate the table rows, also I'm not very familiar with the syntax and all of this. I'm trying to make it work in Yii. which I'm not very familiar also. So the problem is I want to put numbers in front of every row. For example I have 20 table rows and I want to start counting and enumerate them. e.g. 1. George Cat 2. Tom Dog 3. John Bird etc..
Your question is unclear, you do not state what your actual problem is.
Therefore we can only guess...
[...]
echo '<tbody>';
echo '<tr>';
for ($i=0;$i<=40;$i++) {
echo '<td>'.$i.'</td>';
[...]
You probably received a syntax error which you apparently either didn't see (look into the error log files!) or simply ignored. At least you did not post it here though it probably exactly points out what the issue is...

soundcloud http request search engine

I want to retrieve XML data from soundcloud using a simple search engine, that retrieves results in a simple results table array
$tracks = "https://api.soundcloud.com/tracks?q=".$var."&client_id=bbba84be29098bdaad6a5de1c048e3e9&limit=10";
$mysongs = simplexml_load_file($tracks);
echo "<table>";
echo '<table cellpadding="0" cellspacing="0">';
echo '<tbody>';
echo '<tr>';
echo '<th>State</th> <th>Name</th><th>Song</th><th>artist</th><th>user</th> <th>user</th>';
echo '</tr>';
echo '</tbody>';
foreach ($mysongs->track->user as $track) {
echo '<tbody>';
echo '<tr>';
echo '<td>';
echo $user->kind;
echo '</td>';
echo " ";
echo '<td>';
echo $user->kind;
echo " ";
echo '</td>';
echo '<td>';
echo $user->kind;
echo " ";
echo '</td>';
echo '<td>';
echo $user->kind;
echo " ";
echo '</td>';
echo '<td>';
echo $user->kind;
echo '</td>';
echo '<td>';
echo $user->kind;
echo '</td>';
echo '</tr>';
echo '</tbody>';
}
this is my code but nothing seems to display as a result, i have tested the same format of code on another api (yelp) and it does work in xml, so what am i missing here? i dont want to use json, i need it to be xml ( the user -> kind ) is just for testing the code right now.
the variable $user doesn't exist. the variable you want is $track. so where you have
echo $user->kind
replace that with
echo $track->kind
you can see the format of the returned object (and it's hierarchy by running the code in the cli and
print_r($mysongs)

Categories