Different CSS style on last SQL row using PHP - php

Problem:
To style the last row in a SQL query without using any CSS3 selectors.
PHP code:
while ($item = mysql_fetch_assoc($itemsresult))
{
$answer = "SELECT IID, Comment FROM betyg_answers WHERE EID = '{$EID}' AND CID = '{$item['CID']}' ORDER BY ID ASC";
$answerresult = mysql_query($answer) or die ('Error (' . mysql_errno() . ') ' . mysql_error());
$answer = mysql_fetch_assoc($answerresult);
if ($answer['IID'] == $item['IID'])
{
$html .= '
<tr>
<td class="arrow" style="'.$arrow.'"><img src="./img/circle_arrow_right.png" class="arrowimage"></td>
<td class="numbers" style="'.$numbers.'">'.$itemcounter.'.</td>
<td class="text" style="'.$text.'">'.$item['Description'].'</td>
</tr>
';
}
else
{
$html .= '
<tr>
<td class="arrow" style="'.$arrow.'"> </td>
<td class="numbers" style="'.$numbers.'">'.$itemcounter.'.</td>
<td class="text" style="'.$text.'">'.$item['Description'].'</td>
</tr>
';
}
$itemcounter++;
}
Last row in SQL query should instead print:
$html .= '
<tr>
<td class="arrow" style="'.$lastarrow.'"> </td>
<td class="numbers" style="'.$lastnumbers.'">'.$itemcounter.'.</td>
<td class="text" style="'.$lasttext.'">'.$item['Description'].'</td>
</tr>
';
Question:
What needs to be added in the while loop so it recognizes the last row and print a different code instead?

Use a counter:
$i = 0;
$c = mysql_num_rows($itemresult);
while ($item = mysql_fetch_assoc($itemsresult)) {
$i++;
if ($i == $c) {
// This is a last row
} else {
// Regular row
}
}

there is several ways to do that :
use :last-child ( but this isn't working in IE8)
table tr:last-child {
background-color:#ff0000;
}​
using jQuery method ( this is browser independent)
in document load function, add following jQuery code,
$("td:last-child").css({background-color:"#ff0000"})

Related

How to use for loop to print values from array in mpdf PHP

I am using MPDF Library to generate pdf file form database .
i want to show whatsapp number from database in html table TD using array with for loop in MPDF.
here is my code which i try but it cannot print any value .
please help me out to figure out the problem and able to show records using for loop
$query = "SELECT * from social_messaging_app where User_Id = '$split_ids[0]' and Request_Id='$split_ids[1]' and Social_Messaging_App_Name LIKE 'Wh%' ORDER BY Social_Messaging_App_Id DESC";
$res_cus = mysqli_query($connection, $query);
$total_rec_whatsaap = mysqli_num_rows($res_cus);
$whatsapp_accounts = array();
$whatsapp_id = 1;
while ($row = mysqli_fetch_array($res_cus)) {
$whatsapp_accounts[$whatsapp_id] = $row['Social_Messaging_App_No'];
$whatsapp_id++;
}
$html="";
if($total_rec_whatsaap>0){
$html .= '
<table style="width: 100%">
<tbody>
<tr>
<td style="width: 5%;font-size:14px;"></td>
<td style="width: 25%;font-size:14px;">Whatsapp Address(es)</td>
<td style="width: 70%; border-bottom: 1px solid black;text-align: left;font-size:14px;">';
for($i = 1; $i <= $total_rec_whatsaap; $i++)
{
strtoupper($whatsapp_accounts[$i]);
}
$html .= '
</td>
</tr>
</tbody>
</table> ';
}
Please assist me to print values in td. using below code it give me empty row.
You have missed concatenating the string in for loop
for ($i = 1; $i <= $total_rec_whatsaap; $i++)
{
$html .= strtoupper($whatsapp_accounts[$i]);
}

Delete from html table made by database PHP

Here is the table below that I'm trying to delete rows out of:
<form method="POST" >
<table class="sortable">
<thead>
<tr>
<th id="makehead">Make </th>
<th id="modelhead">Model </th>
<th id="idhead">Delete </th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ($carArray as $k => $carInfo) {
$i++;
echo '<tr>';
if ($i % 2) {
echo '<td class="make">' . $carInfo['make'] . '</td>
<td class="model">' . $carInfo['model'] . '</td>
<td class="id"><input type="checkbox" name="id" value="' . $carInfo['id'] . '">' . $carInfo['id'] . '</td>';
} else {
echo '<td class="makelight">' . $carInfo['make'] . '</td>
<td class="modellight">' . $carInfo['model'] . '</td>
<td class="idlight"><input type="checkbox" name="id" value="' . $carInfo['id'] . '">' . $carInfo['id'] . '</td>';
}
}
?>
</tr>
</table>
</tbody>
<td>
<input Onclick="return ConfirmDelete();" name="delete" type="submit" id="delete" value="Delete"></input>
</td>
</table></form>
As you can see i'm using checkboxes to tick each row then the delete button will have a confirm message then should delete but it doesn't here is my if statement:
if ($_REQUEST['delete']) {
$dbid = $_REQUEST['id'];
$db->setdbid($dbid);
So when this wasn't working I had a look on here and on other questions people said I need a setter function so i did this: EDIT: this is my class file.
public function setdbid($dbid){
$this->dbid=$dbid;
}
for this main function to delete things:
public function delete($dbid) {
try {
$sql = "DELETE FROM cars WHERE id = '$dbid'";
$this->db->exec($sql);
echo "Car has been deleted.";
} catch (PDOException $e) {
echo $e->getMessage();
}
}
So that's all the relevant code I think, please help me if you can.
You just have to replace some piece of code in PHP :
if ($_REQUEST['delete']) {
$dbid = $_REQUEST['id'];
$db->delete($dbid); //Assuming delete is well a $db method, else replace it by the correct delete call
}
As you are using checkboxes with the same name, you have to change it so it's an array (and this way you'll be able to delete multiple rows at once) :
<td class="id"><input type="checkbox" name="ids[]" value="' . $carInfo['id'] . '">' . $carInfo['id'] . '</td>';
Then in your php code, treat this data as such :
if ($_REQUEST['delete']) {
foreach($_REQUEST['ids'] as $id){
$xxx->delete(intval($id)); //convert to integer to avoid sql injection.
}
}
Note that you don't need to set $db->setdbid since you pass that id as a parameter of your delete method.

Need to pull data from MySQL data base using PHP to read dynamic checkboxes

I am trying to pull data from mysql table using dynamic checkboxes created previously, however it looks like my search only displays 2 records max and if I select more than 3 checkboxes it will not return anything, so I was wondering if someone could help me figure out how to do it.
Below is my code, I really appreciate your help:
$warehouse = #$_POST['wh'];
switch($button){
case 'Submit':
if(#$_POST['wh']){
$warehouse = #$_POST['wh'];
$length = count($warehouse);
for ($i = 0; $i < $length; $i++){
//echo '<br>'.$warehouse[$i];
}
$consult = "SELECT * FROM contact_info WHERE ";
for ($i = 0; $i < $length; $i++){
$consult = $consult . "warehouse='$warehouse[$i]'";
if(!$i+1 == $length){
$consult = $consult . " OR ";
}
}
echo '<br>'.$length.'<br>';
print_r ($consult);
$response = mysqli_query($connection, $consult);
if($response){
$registry = mysqli_affected_rows($connection);
if($registry > 1){
echo '<br><table width="800" align="center" border="2" cellspacing="1" cellpadding="1">
<form action="index.php" method="post" >
<tr>
<td align="center"><strong>Warehouse</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Phone</strong></td>
<td align="center"><strong>I-net</strong></td>
<td align="center"><strong>E-mail</strong></td>
</tr>';
while($registry = mysqli_fetch_array($response, MYSQLI_ASSOC)){
echo '<form action="index.php" method=post >
<tr>
<td div align="center">'.$registry['warehouse'].'
<td div align="center">'.$registry['name'].'
<td div align="center">'.$registry['lastname'].'
<td div align="center">'.$registry['phone'].'
<td div align="center">'.$registry['inet'].'
<td div align="center">'.$registry['email'].'
</tr>';
}
echo '</form>';
echo '
</form>
</table>';

how to populate dropdown list values from mysql and show it inside the td of html table

I am trying to populate dropdown list values from mysql and show it inside the td of html table,i tried with below code but it not populating values from mysql can any help me how to do that.
<table id="CPH_GridView1" style="width:1452px">
<thead>
<tr>
<th style=" width:102px">Clien ID </th>
<th style=" width:100px">Country</th>
<th style=" width:248px">Network Name </th>
<th style="text-align:center; width:102px" >cppn </th>
</tr>
</thead>
<tbody>
<?php
$sql = mysql_query("SELECT * FROM clientpricenotifications");
while($rows=mysql_fetch_array($sql))
{
if($alt == 1)
{
echo '<tr class="alt">';
$alt = 0;
}
else
{
echo '<tr>';
$alt = 1;
}
echo '<td id="CPH_GridView1_clientid" style="width:140px" class="edit clientid '.$rows["id"].'">'.$rows["clientid"].'</td>
<td id="CPH_GridView1_country" style="width:160px" class="edit country '.$rows['id'].'">'.$rows["country"].'</td>
<td id="CPH_GridView1_networkname" style="width:156px" class="edit networkname '.$rows["id"].'">'.$rows["networkname"].'</td>';
?>
<td>
<select name=' . customer_name . '>
<?php
$query = 'SELECT cppn FROM clientpricenotifications';
$result = mysql_query($query, $db) or die(mysql_error($db));
while ($row = mysql_fetch_assoc($result))
{
echo '<option value="' . $row['id'] . '"> ' . $row['cppn'] . '</option>';
}
?>
</select>
</td>
</tr>'
}
?>
There seems to be a problem with this line:
<td> <select name='customer_name'>
Shouldn't it actually say either this:
<td> <select name="customer_name">
Or:
<td> <select name=' . customer_name . '>
And, that line is part of an echo statement that contains a string in single-quotes, but I can't see where the echo statement's closing single-quote is.
As a result, I think a large bulk of your output is being ignored by the browser because the tag is not being closed properly as some of the output is getting mangled. Check your output with View Source!
If your above code is complete, then I would guess that you're missing the connection to the MySQL server. See: http://www.php.net/manual/en/function.mysql-connect.php
For a related question with code sample, check the answer at: Create table with PHP and populate from MySQL
Not asked, but your table has non matching column widths defined in the styles: Clien(t) ID header 102px, while data cells are 140px.
Another place to look for is following line:
<td style="width:65px" class=" '.$rows["id"].'">
I would expect it should be the following:
<td style="width:65px" class="<?php echo $rows["id"] ?>">
As Vexen Crabtree mentioned, if you also check/post the html code of the HTML output, it would make it easier to diagnose the problem.

Unexpected output when comparing database column to variable

I have code which retrieves information about players from a MySQL database. I want to apply a special case to the HTML output if their ranking changes. I want it to look like this: http://i27.tinypic.com/f406tz.png
But i cant get it to be like i want, instead it prints the rank on every row:
$old_rank = '';
while ($g = mysql_fetch_object($q)) {
if ($g->rankname != $old_rank) {
echo "<tr><td>$g->rankname</td>\n";
$old_rank = "<tr><td> </td>\n";
}
echo " <td>$g->name</td></tr>\n";
}
What I want:
<tr>
<td>One</td>
<td>Kraven the Hunter</td>
</tr>
<tr>
<td> </td>
<td>Kull the Conqueror</td>
</tr>
<tr>
<td> </td>
<td>Zazi The Beast</td>
</tr>
<tr>
<td>Vice-leader</td>
<td>Igos du Ikana</td>
</tr>
<tr>
<td> </td>
<td>Saint Sinner</td>
</tr>
<tr>
<td> </td>
<td>Midvalley the Hornfreak</td>
</tr>.......................
What I get:
<tr><td>One</td>
<td>Tester</td></tr>
<tr><td>One</td>
<td>Kraven the Hunter</td></tr>
<tr><td>One</td>
<td>Kull the Conqueror</td></tr>
<tr><td>One</td>
<td>Zazi The Beast</td></tr>
<tr><td>Vice-Leader</td>
<td>Midvalley the Hornfreak</td></tr>
<tr><td>Vice-Leader</td>
<td>Saint Sinner
</td></tr>
<tr><td>Vice-Leader</td>
<td>Igos du Ikana</td></tr>
$old_rank is never equal to $g->rankname because the way you are setting $old_rank, it will contain HTML tags, and the $g->rankname that you get from the DB will never have HTML tags.
Try changing your if statement to something like this:
if ($g->rankname != $old_rank) {
echo "<tr><td>$g->rankname</td>\n";
$old_rank = $g->rankname;
} else {
echo "<tr><td> </td>\n";
}
It prints the rank name if it's a new rank name, else it prints empty space.
The following (notwithstanding typos) separates out the display logic from the database loop. This has the advantages:
- You don't need to depend on the order of the results returned
- You don't need to maintain dodgy logic (like 'old_rank')
- You can display them more nicely (with a rowspan for repeated ranks
I believe the total code is more compact too.
// fill ranks array
$ranks = array();
while ( $g = mysql_fetch_object($q) ) {
if ( !in_array($g->rankname, $ranks) ) {
$ranks[htmlentities($g->rankname)] = array();
}
$ranks[$g->rankname][] = htmlentities($g->name);
}
// do other program logic here
// end of program
?>
<!-- display the page -->
<table>
<tr>
<th>Rank</th><th>Users</th>
</tr>
<?php foreach($ranks as $rankName => $userList): ?>
<tr>
<td rowspan="<?php echo (string)sizeof($userList); ?>">
<?php echo $rankName; ?>
</td>
<td> <?php echo implode('</td></tr><tr><td>', $userList); ?> </td>
</tr>
<?php endforeach; ?>
</table>
I prefer breaking things up a bit more than that. Keeping things separate makes it easier to modify. This should work.
$old_rank = '';
while ($g = mysql_fetch_object($q)) {
echo '<tr>' . "\n";
echo '<td>';
if ($g->rankname != $old_rank) {
$old_rank = $g->rankname;
echo $old_rank;
} else {
echo ' ';
}
echo '</td>';
echo '<td>' . $g->name . '</td>' . "\n";
echo '</tr>' . "\n";
}

Categories