Adding numeric value count to fetch array php - php

I have query which retrieves and then prints rows of data, but what I am trying to is just add a number in the '$rank' column. So for example if there are 10 rows that come up in the query, I want the query to put a 1 in the first row, 2 in the second and so on...
It is not so much a ranking system, I just want a number to appear in the left column that counts each row. The query I posted below just puts a '1' in the column, can anyone help?
<?php
$bg = '#ffffff'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$i=0;
$rank = $i+1;
$bg = ($bg=='#e1e3e6' ? '#cdcdcf' : '#e1e3e6'); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">';
echo '<td align="center">' . $rank . '</td>';
echo '<td align="center">' . $row['sales_model'] . '</td>';
echo '<td align="center">' . $row['sales_customer_firstname'] . ' ' . $row['sales_customer_surname'] . '</td>';
echo '<td align="center">' . $row['sales_vin'] . '</td>';
echo '<td align="center">' . $row['sales_rda'] . '</td>';
echo '<td align="center">' . $row['sales_commission_no'] . '</td>';
echo '<td align="center">' . $row['sales_points'] . '</td>';
}
?>

Here is an example
$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
/* while code here */
++$i; //increment the counter by 1
}

Related

How to send a variable value with HREF in PHP

i have one table in locallhost ,In my table there is an item id,Now I want to send the id Related row to another page by clicking on EDIT . Thankful
The part of the program I think is difficult to write below
....
....
$response["travel"]=array();
while ($row = mysql_fetch_array($sql)) {
// Print out the contents of the entry
echo '<tr>';
echo '<td class="text-center">' . $i . '</td>';
echo '<td class="text-center">' . $row['companyname'] . '</td>';
echo '<td class="text-center">' . $row['cod'] . '</td>';
echo '<td class="text-center">' . $row['bigan'] . '</td>';
echo '<td class="text-center">' . $row['stop'] . '</td>';
echo '<td class="text-center">' . $row['date'] . '</td>';
echo '<td class="text-center">' . $row['time'] . '</td>';
echo '<td class="text-center">' . $row['price'] . '</td>';
echo '<td class="text-center">' .'EDIT' .'</td>';
$i++;
.......
.....
You don't use <?php inside strings, you use that when you've gone back into HTML mode. You should just concatenate the variable, like you do everywhere else
echo '<td class="text-center">' .'ویرایش' .'</td>';

How can I add a class for td in reference to two columns

I have a render of row of database
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['tex1'] . '</td>';
echo '<td>' . $row['tex2'] . '</td>';
echo '<td>' . $row['tex3'] . '</td>';
echo '<td>' . $row['tex4'] . '</td>';
echo '<td>' . $row['tex5'] . '</td>';
echo '<td>' . $row['tex6'] . '</td>';
echo '<td>' . $row['tex7'] . '</td>';
echo '<td>' . $row['tex8'] . '</td>';
echo '<td>' . $row['tex9'] . '</td>';
echo '<td>' . $row['tex10'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
And in the tex1 is a date, my problem is for a class for tr, when the tex2 is empty and the date of tex1 is older than 30 days, tr have class red, the date of tex1 is inserted manually,how can I do to change the class on the basis of this?
I'm not sure if I understood what you wanted properly but I'll give it a shot. You want to add a class ("red") to the , whenever the condition is met that either the date is older than 30 days old (tex1) or the First Name is empty (tex2).
echo "<table border='1' cellpadding='10'>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
// I assume since you're just printing it out, the date is a String
// So first convert it to a timestamp
$date = strtotime($row['tex1']);
// Determine the difference, I'm doing it this way to make it easy for you to understand
$days = 60*60*24*30; // 30 days
if (empty($row['tex2']) || time()-$date > $days) {
echo '<tr>';
} else {
echo '<tr class="red">';
}
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['tex1'] . '</td>';
echo '<td>' . $row['tex2'] . '</td>';
echo '<td>' . $row['tex3'] . '</td>';
echo '<td>' . $row['tex4'] . '</td>';
echo '<td>' . $row['tex5'] . '</td>';
echo '<td>' . $row['tex6'] . '</td>';
echo '<td>' . $row['tex7'] . '</td>';
echo '<td>' . $row['tex8'] . '</td>';
echo '<td>' . $row['tex9'] . '</td>';
echo '<td>' . $row['tex10'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
You should check for the conditions what you have mentioned in your question. And this can be done like this.
if(strtotime($row['tex1']) + 30 * 24 * 60 * 60 < time() && $row['tex2'] =="") {
$class="class='red'";
} else {
$class= "";
}
echo "<tr $class>";// id condition satisfies, class='red' will get echo, else echo will be null.
Try a conditional when writing the element:
echo '<tr'.
( ((strtotime($row['tex1']) < strtotime('30 days ago'))
&& (empty($row['tex2']))) ? ' class="red"' : '').
'>';

How to highlight rows in php table?

I have table called reservations. It displays reservations made by users. I want highlight records in current date using end date.
Php code
$result2 = mysql_query("SELECT * FROM reservations WHERE hotel_id = '1' ORDER BY end");
while ($row = mysql_fetch_array($result2)) {
echo '<tr>';
echo '<td class="contacts">' . $row['fname'] . ' ' . $row['lname'] . '</td>';
echo '<td class="contacts">' . $row['start'] . '</td>';
echo '<td class="contacts">' . $row['end'] . '</td>';
echo '<td class="contacts">' . $row['qty'] . '</td>';
echo '</td>';
echo '<td class="contacts">' . $row['room_type'] . '</td>';
echo '<td class="contacts">' . '<a href=out.php?id=' . $row["res_id"] . '>' . 'Check Out' . '</a>' . '</td>';
echo '</tr>';
}
I'd do that at frontend side, but if you want to compute than in PHP, you will need to compare the date from $row and current date and add a class or a style tag to the .
Like so:
$rowHasCurrentDate = $row['date'] == date('Y-m-d');
echo '<tr' + ($rowHasCurrentDate ? ' class="highlight-row"' : "") + '>';
instead of
echo '<tr>';
You can replace the 'class="highlight-row"' with 'style="background-color:red"' if you want to do it without touching frontend side at all.
The example is kinda spaghetti-code, but you can move this logic somewhere else after you get it working.
I am assuming that your start date is current date. This is css thing, i have given you solution to you.
<html>
<head>
<style>
.currdate
{
background-color:red; //change this color to whatever you wish to change to
}
</style>
</head>
<body>
<?php
$result2 = mysql_query("SELECT * FROM reservations WHERE hotel_id = '1' ORDER BY end");
while ($row = mysql_fetch_array($result2)) {
echo '<tr>';
echo '<td class="contacts">' . $row['fname'] . ' ' . $row['lname'] . '</td>';
echo '<td class="contacts currdate">' . $row['start'] . '</td>';
echo '<td class="contacts">' . $row['end'] . '</td>';
echo '<td class="contacts">' . $row['qty'] . '</td>';
echo '</td>';
echo '<td class="contacts">' . $row['room_type'] . '</td>';
echo '<td class="contacts">' . '<a href=out.php?id=' . $row["res_id"] . '>' . 'Check Out' . '</a>' . '</td>';
echo '</tr>';
}
?>
<body>
</html>

php replacing query duplicates for empty strings

i have the following script to retrieve sql data:
// Present data if avail
if ($result->num_rows > 0) {
// Query output in tabels
while($row = $result->fetch_assoc()) {
echo '<tr> <td class="tg">' .$row["id"]. '</td>' . "\n";
echo '<td class="tg">' .$row["fname"]. '</td>' . "\n";
echo '<td class="tg">' .$row["lname"]. '</td>' . "\n";
echo '<td class="tg">' .$row["make"]. '</td></tr>' . "\n";
}
// if no records
} else {
echo "0 results";
}
How can i replace duplicates in the row 'fname' for empty strings? (records are being ordered by fname)
The script you showed us doesn't retrieve SQL data - it just displays it. If you managed to write the code above then this should be obvious:
$prevname='';
while($row = $result->fetch_assoc()) {
echo '<tr> <td class="tg">' .$row["id"]. '</td>' . "\n";
echo '<td class="tg">'
. ( $row["fname"] == $prevname ? '' : $row["fname"])
. '</td>' . "\n";
echo '<td class="tg">' .$row["lname"]. '</td>' . "\n";
echo '<td class="tg">' .$row["make"]. '</td></tr>' . "\n";
$prevname=$r["fname"];
}

PHP Mysql_fetch_assoc is Omitting last row

In PHP MYSQL_FETCH_ASSOC is omitting Last Row. This never happened. But this time it put me into soup at the last moment.
Even I've put up mysql_num_rows the result is 14 records -- but on list it shows only 13 records and the 14th record is omitted.
Any kind of help is Appreciated.
$uno = $_GET["uno"];
$xtc1 = 'select * from rform where uno="' . $uno . '" order by rno DESC';
$xtc = mysql_query($xtc1) or die('User Reservation Retrival Error : ' . mysql_error());
$trno = mysql_fetch_assoc($xtc);
$trow = mysql_num_rows($xtc);
echo '<p>List of Onlilne Reservations made by <strong style="font-weight:bold; color:red;">' . ucwords($trno["cname"]) . ' (' . $trow . ')</strong></p>';
echo '<table cellpadding="5" cellspacing="0" border="1">';
echo '<tr>';
echo '<td colspan="5" style=" font-size:14px; text-align:center; font-weight:bold; color:red;">' . ucwords($trno["cname"]) . '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>R.NO</th>';
echo '<th>From</th>';
echo '<th>To</th>';
echo '<th>Date & Time of<Br>Travel</th>';
echo '<th>Reserved On</th>';
echo '</tr>';
while($mtn = mysql_fetch_assoc($xtc)){
$dt = $mtn["csdate"] . ' ' . $mtn["ctime"];
echo '<tr>';
echo '<td>' . $mtn["rno"] . '</td>';
echo '<td>' . $dt . '</td>';
echo '<td>' . $mtn["caddr"] . '</td>';
echo '<td>' . $mtn["cdest"] . '</td>';
echo '<td>' . date('d-M-Y',strtotime($mtn["tstamp"])) . '</td>';
echo '</tr>';
}
echo '</table>';
You have an extra $trno = mysql_fetch_assoc($xtc) that you sem to be discarding. This is your missing row. Just remove that line.
deleting the first $trno = mysql_fetch_assoc($xtc); will solve this problem.
In case you need to read the first line of $xtc before the loop. You can change while loop to do-while, without deleted the first $mtn = mysql_fetch_assoc($xtc);
do{
//whatever
}while($mtn = mysql_fetch_assoc($xtc));

Categories