I have table with informations from database. There are ID, Name, Subject and Date. And I want to show more data like Message, OS, IP etc. when I click on one line (tr) of table. I found solution, but it is woking only on the first tr, not all.
Here is my code:
while($row = $result->fetch_assoc())
{
$id=$row['ID'];
$name=$row['Name'];
$mail=$row['Email'];
$subject=$row['Subject'];
$message=$row['Message'];
$date=$row['Date'];
$ip=$row['IP'];
$device=$row['Device'];
$os=$row['OS'];
$browser=$row['Browser'];
$finish=$row['Finish'];
echo
'<tr class="trX" id="flip">
<td class="tdX">' . $id . '</td>
<td class="tdX">' . $name . '</td>
<td class="tdX">' . $subject . '</td>
<td class="tdX">' . $date . '</td>
<div id="panel">
ID:' . $id . '
Name:' . $name . '
Email:' . $mail . '
Subject:' . $subject . '
Message:' . $message . '
Date:' . $date . '
IP:' . $ip . '
Mobile:' . $device . '
OS:' . $os . '
Browser:' . $browser . '
Finish:' . $finish . '
</div>
</tr>';
}
echo ' </table> ';
And JS Query
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideToggle("slow");
});
});
Thanks for tips
It's only working on first tr because you can not reuse the ID flip for other elements. Few solutions I can think of:
Have a variable which increments and assign it to the id like flip-i, i here is the incrementing variable. Attach click event to this and then toggle the child elements.
Attach click event to a class and then toggle children of that clicked class.
I changed your code this way:
<tr class="trX" id="flip_".$i onclick="flippanel($i)">
<td class="tdX">' . $id . '</td>
<td class="tdX">' . $name . '</td>
<td class="tdX">' . $subject . '</td>
<td class="tdX">' . $date . '</td>
<div id="panel_".$i>
ID:' . $id . '
Name:' . $name . '
Email:' . $mail . '
Subject:' . $subject . '
Message:' . $message . '
Date:' . $date . '
IP:' . $ip . '
Mobile:' . $device . '
OS:' . $os . '
Browser:' . $browser . '
Finish:' . $finish . '
</div>
</tr>
In jQuery you can call this like
function flippanel(i)
Then you can access panel by individual ID easily
Related
echo "
<table class='productNeedTb'>
<tr>
<td >{$row['O_no']}</td>
<td style='color:{$statusColor};'>{$row['Status']}</td>
<td>{$row['Product']}</td>
<td>{$row['Quantity']}</td>
<td>{$row['Price']}</td>
<td>{$row['Place']}</td>
<td>{$row['Fee']}</td>
<td>{$row['Dest']}</td>
<td>{$row['ExpeTime']}</td>
<td>{$row['OtherNeed']}</td>
<td>{$row['Req_date']}</td>
<td><button name='IcanBtn' type='submit' class='IcanBtn' onclick='showHelper(`{$row['O_no']}`, `{$row['Product']}`, `{$row['Quantity']}`, `{$row['Price']}`, `{$row['Place']}`, `{$row['Fee']}`, `{$row['Dest']}`, `{$row['ExpeTime']}`, `{$row['OtherNeed']}` )'>我要接單!</button></td>
</tr>
</table>
<br>
";
I wrote an php code but in the last td we have button with a function showHelper() which is supposed to pass value to the function and do something else.
and this is thw erroe that I got :
麥當勞大麥克 undefined undefined undefined undefined undefined undefined undefined undefined
I know the problem would be in {$row['O_no']}, {$row['Product']} the ,here but I just can't fix it.
I tried to pass it without quotes but it sees it as a variable but it's supposed to be string so error occur so I tried to make them all string and using "" or '' cause some sort problem as well
if(isset($_SESSION['u_uid']))
{
//create a prepared statement
$sql="SELECT Status, O_no, Product, Quantity, Price, Place, Fee, Dest, ExpeTime, OtherNeed, Req_date FROM productneeds WHERE Status='等待中' or Status='進行中' ORDER BY O_no DESC;";
$exe=mysqli_query($conn, $sql);
$exeChk=mysqli_num_rows($exe);
if($exeChk>0)
{
echo "
<table class='productNeedTb'>
<tr>
<td>訂單No.</td>
<td>狀態</td>
<td>物品</td>
<td>物品數量</td>
<td>物品單價</td>
<td>購買地點</td>
<td>願意支付跑腿費</td>
<td>送達地點</td>
<td>期望送達時間</td>
<td>特殊要求</td>
<td>提出時間</td>
<td></td>
</tr>
</table>
<br>
";
while($row=mysqli_fetch_assoc($exe))
{
//$p_info=[$row['O_no'],$row['Product'],$row['Quantity'],$row['Price'],$row['Place'],$row['Fee'],$row['Dest'],$row['ExpeTime'],$row['OtherNeed']];
if($row['Status']=="等待中")
{
$statusColor="red";
echo "
<table class='productNeedTb'>
<tr>
<td >{$row['O_no']}</td>
<td style='color:{$statusColor};'>{$row['Status']}</td>
<td>{$row['Product']}</td>
<td>{$row['Quantity']}</td>
<td>{$row['Price']}</td>
<td>{$row['Place']}</td>
<td>{$row['Fee']}</td>
<td>{$row['Dest']}</td>
<td>{$row['ExpeTime']}</td>
<td>{$row['OtherNeed']}</td>
<td>{$row['Req_date']}</td>
<td>
<button name='IcanBtn' type='submit' class='IcanBtn' onclick='showHelper(\" " . $row['O_no'] . " \", \" " . $row['Product'] . " \",\"" . $row['Quantity'] . "\",\"" . $row['Price'] . "\",\"" . $row['Place'] . "\",\"" . $row['Fee'] . "\",\"" . $row['Dest'] . "\",\"" . $row['ExpeTime'] . "\",\"" . $row['OtherNeed'] . "\")'>我要接單!</button>
</td>
</tr>
</table>
<br>
";
}
The reason why your attempt was unsuccessful was because javascript saw your parameters as javascript variables hence the undefined error.
To fix your issue, i wrapped the variables around quotes and escaped to prevent them from being seen as javascript variables
<td><button name='IcanBtn' type='submit' class='IcanBtn' onclick='showHelper(\"" . $row['O_no'] . "\",\"" . $row['Product'] . "\",\"" . $row['Quantity'] . "\",\"" . $row['Price'] . "\",\"" . $row['Place'] . "\",\"" . $row['Fee'] . "\",\"" . $row['Dest'] . "\",\"" . $row['ExpeTime'] . "\",\"" . $row['OtherNeed'] . "\")'>我要接單!</button></td>
I don't know how to describle it, just look at the code:
$html =
'<table>
<tr>
<td>'
if(strlen($parse['abweichend_name']) > 2)
{
'Lieferanschrift<br>' . $parse['abweichend_firma'] . '<br>' . $parse['abweichend_name'] . '<br>' . $parse['abweichend_strasse'] . '<br>' . $parse['abweichend_plz'] . ' ' . $parse['abweichend_ort'];
}
else
{
'Lieferanschrift<br>' . $parse['firma'] . '<br>' . $parse['name'] . '<br>' . $parse['strasse'] . '<br>' . $parse['plz'] . ' ' . $parse['ort'];
}
'</td>
<td align="right" valign="top">
<font size="5" color="#808080">bla</font>
</td>
</tr>
</table>';
Is it even possible to do it somehow? xD I know how to do it right/normally but just wonder if there is any way to do it this way
In your case, as all fields have prefix abweichend_ you can do this:
$html = '<table><tr><td>';
$prefix = strlen($parse['abweichend_name']) > 2? 'abweichend_' : '';
$html .= 'Lieferanschrift<br>' . $parse[$prefix . 'firma'] . '<br>' . $parse[$prefix . 'name'] . '<br>' . $parse[$prefix . 'strasse'] . '<br>' . $parse[$prefix . 'plz'] . ' ' . $parse[$prefix . 'ort'];
$html .= '</td><td align="right" valign="top"><font size="5" color="#808080">bla</font></td></tr></table>';
And of course, I replaced $html = $html . "string" with $html .= "string".
You can use a normal if condition and append to the string variable.
$html = '<table><tr><td>';
if(strlen($parse['abweichend_name']) > 2) {
$html = $html . 'Lieferanschrift<br>' . $parse['abweichend_firma'] . '<br>' . $parse['abweichend_name'] . '<br>' . $parse['abweichend_strasse'] . '<br>' . $parse['abweichend_plz'] . ' ' . $parse['abweichend_ort'];
} else {
$html = $html . 'Lieferanschrift<br>' . $parse['firma'] . '<br>' . $parse['name'] . '<br>' . $parse['strasse'] . '<br>' . $parse['plz'] . ' ' . $parse['ort'];
}
$html = $html . '</td><td align="right" valign="top"><font size="5" color="#808080">bla</font></td></tr></table>';
Or you can use the ternary operator, but in my opinion the readability of this solution is worse.
$html = '<table><tr><td>' .
(strlen($parse['abweichend_name']) > 2) ?
('Lieferanschrift<br>' . $parse['abweichend_firma'] . '<br>' . $parse['abweichend_name'] . '<br>' . $parse['abweichend_strasse'] . '<br>' . $parse['abweichend_plz'] . ' ' . $parse['abweichend_ort']) :
('Lieferanschrift<br>' . $parse['firma'] . '<br>' . $parse['name'] . '<br>' . $parse['strasse'] . '<br>' . $parse['plz'] . ' ' . $parse['ort']) .
'</td><td align="right" valign="top"><font size="5" color="#808080">bla</font></td></tr></table>'
I've been trying to work out how to create a table using php and mysql, this is what i've got so far...
PHP :-
foreach($result as $res){
echo '<tr><td style="display:none;"><p id="ID">' . $ID . ' </p></td><td>' . $res['NAME'] . '</td>
<td>' . $res['option2'] . '</td>
<td>' . $res['Option3'] . '</td>
<td>' . $res['Option4'] . '</td>
<td>' . $res['Option5'] . '</td>
<td class="deleteBtn"><button class="btn btn-danger btn-xs" id="deleteBtn">Delete</button></td></tr>';
}
Javascript:-
$('button#deleteBtn').click( function(){
data = $('p#ID').html();
alert(data);
// window.location('admin.php?link=LF1'+data);
});
when i click deleteBtn i want to delete the row the button is on, therefore i need the ID...
How do I get the value of $ID. my jquery alerts only the first ID for every button...
Thanks!
First of all, id should be unique, so you cannot do id="ID" for every row.
Try with:
foreach($result as $res){
echo '<tr data-id="' . $ID . '">';
// ...
echo '</tr>';
}
And in JavaScript:
$('button#deleteBtn').click( function(){
var id = $(this).parents('tr').data('id');
alert(id);
}
It looks like if you have more than 1 row, you'll inevitably end up with a number of elements with the same ID. A cleaner up version would be:
More easily, you can simply use .closest()
PHP:
echo '<tr id='.$ID.'><td style="display:none;"><p id="ID">' . $ID . ' </p></td><td>' . $res['NAME'] . '</td>
<td>' . $res['option2'] . '</td>
<td>' . $res['Option3'] . '</td>
<td>' . $res['Option4'] . '</td>
<td>' . $res['Option5'] . '</td>
<td class="deleteBtn"><button class="delete btn btn-danger btn-xs">Delete</button></td></tr>'
jQuery:
$('button.delete').click( function(){
var toDelete = $(this).closest('tr').attr('id');
alert(toDelete);
});
You can use data attribute to keep your id. like data-id="1"
foreach($result as $res) {
echo '<tr><td style="display:none;"><p data-id="'.$ID.'" id="ID"'.$ID.' data-id="">' . $ID . ' </p></td><td>' . $res['NAME'] . '</td>
<td>' . $res['option2'] . '</td>
<td>' . $res['Option3'] . '</td>
<td>' . $res['Option4'] . '</td>
<td>' . $res['Option5'] . '</td>
<td class="deleteBtn"><button onclick="delete("ID'.$ID.')" class="btn btn-danger btn-xs" id="deleteBtn">Delete</button></td></tr>';
}
Now you can Id like this:-
$('button#deleteBtn').click( function(){
data = $(this).parents('tr').data('id');
alert(data);
});
Im wondering if its possible with PHP to add a class to X record returned. I know I can do this with JS only I'd like it to have the class added as the records are returned.
I have the following loop in my PHP, From what I've found in google I need to add a counter to do this only I've been unsuccessful so far...
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo '<div class="entry span3"><span class="name">' . $row['First_Name'] . ' ' . $row['Surname'] . "</span>";
echo '<img src="' . $row["picture_1"] . '" alt="' . $row['First_Name'] . ' ' . $row['Surname'] . ', text ' . $row['Date'] . ' ">';
echo '<span class="from">seen in ' . ucwords($row["Location_County__Seen"]) . '</span>View Profile</div>';
}
In front of your while, add $c = 1
Before the end of your while loop, add $c++;
Then, modify your first line:
echo '<div class="entry span3"><span class="name">'
To
echo '<div class="entry span3';
if (($c % 4) == 1) echo ' newclassname ';
echo '"><span class="name">'
For the final result:
$c = 1;
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo '<div class="entry';
if (($c % 4) == 1) echo ' newclassname ';
echo ' span3"><span class="name">' . $row['First_Name'] . ' ' . $row['Surname'] . "</span>";
echo '<img src="' . $row["picture_1"] . '" alt="' . $row['First_Name'] . ' ' . $row['Surname'] . ', text ' . $row['Date'] . ' ">';
echo '<span class="from">seen in ' . ucwords($row["Location_County__Seen"]) . '</span>View Profile</div>';
$c++;
}
I am looking for simple, the best practical way of placing summed value above HTML table.
Normally loops can generate sums after the loop is finished that is below the table of all results. It may be very difficult when the table is very long and we need to scroll down every time to see the sum of sales. Do I need to run two loops or is there any trick to do that? Task seems very simple. In below code I am summing Quantities from all orders and Total Sales values:
$lp=1; $total=0; $total_qt=0;
while ($record = mysql_fetch_array($result)){
$total += $record['Total'];
$total_qt += $record['Qt'];
echo '<tr style="background-color:'. ((next($colour) == true) ? current($colour) : reset($colour)).'">
<td> ' . $lp++ . '</td>
<td class="bolder"> ' . $record['Name'] . '</td>
<td> ' . $record['Product'] . '</td>';
if ($invoice=="on") echo '<td style="font-size:11px">' . $record['Invoice'] . '</td>';
echo '<td> ' . $record['despatched'] . '</td>
<td> ' . $record['Qt'] . '</td>
<td> ' . $record['Price'] . '</td>
<td class="align_right"> ' . $record['Total'] . '</td>';
echo '</tr>';
}
echo '</table><div class="stat_total">Qt: '.$total_qt.' Total: £'.$total.'</div> </div>';
You could use a variable to store your output while you are in the loop, rather than echo it, and when you finished the loop then echo the stored results with both totals before and after them. Like this:
$lp=1; $total=0; $total_qt=0;
$output = ''; //here you temporarily save your output
while ($record = mysql_fetch_array($result)){
$total += $record['Total'];
$total_qt += $record['Qt'];
$output .= '<tr style="background-color:'. ((next($colour) == true) ? current($colour) : reset($colour)).'">
<td> ' . $lp++ . '</td>
<td class="bolder"> ' . $record['Name'] . '</td>
<td> ' . $record['Product'] . '</td>';
if ($invoice=="on") $output .= '<td style="font-size:11px">' . $record['Invoice'] . '</td>';
$output .= '<td> ' . $record['despatched'] . '</td>
<td> ' . $record['Qt'] . '</td>
<td> ' . $record['Price'] . '</td>
<td class="align_right"> ' . $record['Total'] . '</td>';
$output .= '</tr>';
}
//Now you can put a div with the Total both at the start and end of the table
echo '<div class="stat_total">Qt: '.$total_qt.' Total: £'.$total.'</div>';
echo $output . '</table>';
echo '<div class="stat_total">Qt: '.$total_qt.' Total: £'.$total.'</div> </div>';
By using the SUM() SQL function you'll be able to access the sums without needing to run PHP:
SQL
SELECT SUM(Total) FROM Orders