Enumerate table rows in html - php

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...

Related

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

how do I update the value of each row without primary key?

I want to update the value in a certain row. But it seems impossible since this table (from database) doesn't have the primary key. It only have two foreign key in its column. What should I do ? please, any help ?
here is my code :
<table align= "center" border="1" cellspacing="0" cellpadding="2">
<?php
$strgettable="select tblUsrGrp.UsrGrpNama, tblmenu.MenuNama, tblmenuakses.MenuAkses
FROM tblUsrGrp
inner join tblMenuAkses
on tblUsrGrp.UsrGrpID = tblMenuAkses.usrgrpid
inner join tblMenu
on tblMenu.menuid = tblMenuAkses.menuid";
$varRecCount=0;
$rs=odbc_exec($dbconnVOT, $strgettable);
if($rs)
{
echo '<tr><td style=width:10%;>';
echo '<center><b> No. </b></center>';
echo '</td>';
echo '<td style=width:30%;>';
echo '<center><b> User Group </b></center>';
echo '</td>';
echo '<td style=width:20%;>';
echo '<center><b> Menu Name </b></center>';
echo '</td>';
echo '<td style=width:20%;>';
echo '<center><b> Menu Akses </b></center>';
echo '</td>';
echo '<td style=width:20%;>';
echo '<center><b> Update </b></center>';
echo '</td></tr>';
while (odbc_fetch_row($rs))
{
$varRecCount++;
echo '<tr><td>';
echo '<center> '.$varRecCount.'</center>';
echo '</td>';
echo '<td>';
echo '<center> '.odbc_result($rs,"UsrGrpNama").'</center>';
echo '</td>';
echo '<td>';
echo '<center> '.odbc_result($rs,"MenuNama").'</center>';
echo '</td>';
echo '<td>';
echo '<center> '.odbc_result($rs,"MenuAkses").'</center>';
echo '</td>';
echo '<td>';
echo '</td>';
echo '</td></tr>';
}
}
?>
</table>
<div style="text-align:center;">
<input type="button" name="btnUpdate" id="btnUpdate" onClick="javascript:sendData('updatedata');" value = "Update">
</div>
any help would be much appreciated . TQ

How do I show database info on my website?

I have a account page on my website where it shows all information about a user but it does not show the username.
Database Name: df
Table to get info from: members
Column that stores all the usernames: user
Screenshot:
So I tried this code but it did not work. You may wonder how I came up with that code: I copied the other code that shows when user is registered.
echo '<tr>';
echo '<td align="left" class="padding_td" width="'.$td_width.'">';
echo 'Registrerad:';
echo '</td>';
echo '<td align="left" class="padding_td">';
echo members($user['user']);
echo '</td>';
echo '</tr>';
When I try this code on my site I get this error:
Fatal error: Call to undefined function members() in
C:\xampp\htdocs\account.php on line 328
Here's the code to show when user is registered (may or may not be helpful):
echo '<tr>';
echo '<td align="left" class="padding_td" width="'.$td_width.'">';
echo 'Registrerad:';
echo '</td>';
echo '<td align="left" class="padding_td">';
echo date_detailed($user['date_registred']);
echo '</td>';
echo '</tr>';
Try this:
echo '<tr>';
echo '<td align="left" class="padding_td" width="'.$td_width.'">';
echo 'Registrerad:';
echo '</td>';
echo '<td align="left" class="padding_td">';
echo $user['user'];
echo '</td>';
echo '</tr>';

Table problems in 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.

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