PHP looping out records within seperate JQuery accordions - php

I have the following MySQL query:
$query_waitingrooms = "SELECT * FROM waitingroom, waitingroom_case_lookup, vcase, patient WHERE vcase.patient_fk = patient.patient_pk AND waitingroom.waitingroom_pk = waitingroom_case_lookup.waitingroom_fk AND vcase.case_pk = waitingroom_case_lookup.case_fk AND vcase.active = 'y' AND vcase.case_pk NOT IN (SELECT case_fk FROM user_case_lookup WHERE user_id = '$user_id')";
$result_waitingrooms = mysql_query($query_waitingrooms, $connection) or die(mysql_error());
The result is printed:
while ($row_waitingrooms = mysql_fetch_assoc($result_waitingrooms)){
echo "<h6>" . $row_waitingrooms['waitingroom'] . "</h6><div><p><span class='text'><table width='100%'><tr><td><input name='case' id='case_" . $row_waitingrooms['case_pk'] . "' type='radio' value='" . $row_waitingrooms['case_pk'] . "' /></td><td width='65' align='left'><div class='case_list'><img src='images/case_icons/" . $row_waitingrooms['patient_icon'] . "' width='44' height='45' /></div></td><td width='350' align='left'>" . $row_waitingrooms['first_name'] . ' ' . $row_waitingrooms['last_name'] . ' &nbspGender: ' . $row_waitingrooms['gender'] . ' Age: ' . $row_waitingrooms['age'] . '<br />Presenting Complaint: ' . $row_waitingrooms['presenting_complaint'] . "</td></tr></table></p></div>";
}
As expected, this results in an accordion for each of the records in table 'waitingroom_case_lookup' containing one 'case' per accordion. What I'm after is to have an accordion for each 'waitingroom' and each containing the relevant case data from the tables vcase and patient.
At the moment the table 'waitingroom_case_lookup' is:
CREATE TABLE waitingroom_case_lookup (
waitingroom_case_lookup_pk int(9) NOT NULL AUTO_INCREMENT,
waitingroom_fk int(3) NOT NULL,
case_fk int(3) NOT NULL,
PRIMARY KEY (waitingroom_case_lookup_pk)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table 'waitingroom_case_lookup'
--
INSERT INTO waitingroom_case_lookup (waitingroom_case_lookup_pk, waitingroom_fk, case_fk) VALUES
(1, 1, 1),
(2, 1, 2),
(3, 2, 3);
It would appear that I need to loop out the cases in each of the accordion waitingrooms between <span class='text'> and </p></div> Any suggestions how I can do this?
EDIT
Thanks to NoBBY I have the following working code (still need to clean it up re formatting...):
<?php
while ($row_waitingrooms = mysql_fetch_assoc($result_waitingrooms)){
$waitingRoomPK = $row_waitingrooms['waitingroom_pk'];
if (!isset($waitingRooms[$waitingRoomPK])) {
$waitingRooms[$waitingRoomPK] = array(
'waitingroom_pk' => $waitingRoomPK,
'waitingroom' => $row_waitingrooms['waitingroom'],
'cases' => array()
);
}
$waitingRooms[$waitingRoomPK]['cases'][] = array(
'case_pk' => $row_waitingrooms['case_pk'],
'patient_icon' => $row_waitingrooms['patient_icon'],
'first_name' => $row_waitingrooms['first_name'],
'last_name' => $row_waitingrooms['last_name'],
'age' => $row_waitingrooms['age']
);
echo "<h6>" . $row_waitingrooms['waitingroom'] . "</h6><div><p><span class='text'>";
foreach ($waitingRooms[$waitingRoomPK]['cases'] as $wcase){
echo "<table width='100%'><tr><td><input name='case' id='case_" . $wcase['case_pk'] . "' type='radio' value='" . $wcase['case_pk'] . "' /></td><td width='65' align='left'><div class='case_list'><img src='images/case_icons/" . $wcase['patient_icon'] . "' width='44' height='45' /></div></td><td width='350' align='left'>" . $wcase['first_name'] . ' ' . $wcase['last_name'] . ' &nbspGender: ' . $wcase['gender'] . ' Age: ' . $wcase['age'] . '<br />Presenting Complaint: ' . $wcase['presenting_complaint'] . "</td></tr></table>";
}
echo "</span></p></div>";
}
?>

Instead of directly ouputting the result from the database try formatting i t first.
For example you can do something like that:
$waitingRooms = array();
while ($row_waitingrooms = mysql_fetch_assoc($result_waitingrooms)){
$waitingRoomPK = $row_waitingrooms['waitingroom_pk'];
if (!isset($waitingRooms[$waitingRoomPK])) {
$waitingRooms[$waitingRoomPK] = array(
'waitingroom_pk' => $waitingRoomPK,
'waitingroom' => $row_waitingrooms['waitingroom'],
'cases' => array(),
//.....all the data for a waitingroom
);
}
$waitingRooms[$waitingRoomPK]['cases'][] = array(
'case_pk' => $row_waitingrooms['case_pk'],
'patient_icon' => $row_waitingrooms['patient_icon'],
'first_name' => $row_waitingrooms['first_name'],
//.......all the data for a patient
);
}
Now $waitingRooms is array with data for each waiting room, and a 'cases' subarray with data for each patient in the room
From here 2 simple nested foreach statements will output the desired html for the accordions
Here is some ways to improve your code though:
Do not use mysql_* functions, they are deprecated. Those huge red warnings in the docs are not just for looks. User mysqli or PDO (both linked in the docs)
Use actual joins, that alternative join syntax in the query hurts readability
Format your sql and html code. I bet lots of people skipped your question when they saw these horisontal scrollbars in the code segments
I hope you will agree that that:
<?php while($row_waitingrooms = mysql_fetch_assoc($result_waitingrooms)): ?>
<h6><?= $row_waitingrooms['waitingroom']; ?></h6>
<div>
<p>
<span class='text'>
<table width='100%'>
<tr>
<td>
<input name='case' id='case_<?= $row_waitingrooms['case_pk']; ?>' type='radio' value='<?= $row_waitingrooms['case_pk']; ?>' />
</td>
<td width='65' align='left'>
<div class='case_list'>
<img src='images/case_icons/<?= $row_waitingrooms['patient_icon']; ?>' width='44' height='45' />
</div>
</td>
<td width='350' align='left'>
<?= $row_waitingrooms['first_name'] . ' ' . $row_waitingrooms['last_name']; ?>
Gender: <?= $row_waitingrooms['gender']; ?>
Age: <?= $row_waitingrooms['age']; ?>
<br />
Presenting Complaint: <?= $row_waitingrooms['presenting_complaint']; ?>
</td>
</tr>
</table>
</p>
</div>
<?php endwhile; ?>
is way better than that:
while ($row_waitingrooms = mysql_fetch_assoc($result_waitingrooms)){
echo "<h6>" . $row_waitingrooms['waitingroom'] . "</h6><div><p><span class='text'><table width='100%'><tr><td><input name='case' id='case_" . $row_waitingrooms['case_pk'] . "' type='radio' value='" . $row_waitingrooms['case_pk'] . "' /></td><td width='65' align='left'><div class='case_list'><img src='images/case_icons/" . $row_waitingrooms['patient_icon'] . "' width='44' height='45' /></div></td><td width='350' align='left'>" . $row_waitingrooms['first_name'] . ' ' . $row_waitingrooms['last_name'] . ' &nbspGender: ' . $row_waitingrooms['gender'] . ' Age: ' . $row_waitingrooms['age'] . '<br />Presenting Complaint: ' . $row_waitingrooms['presenting_complaint'] . "</td></tr></table></p></div>";
}
And you get the bonus to see the unclosed span tag :)

Related

pass value in javascript function along with php

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>

How come my query works in phpmyadmin but not in my php?

I have a query I'm trying to run in my php code and it is only returning one result, but if i run the same query in phpmyadmin it works. Can anyone tell me where I'm going wrong?
<?php
$sql = "SELECT * FROM `product_packs` WHERE `name` IN('" . implode("', '", $_SESSION['cart_items']) . "')";
$result = $conn->query($sql);
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "<div class='col-xs-6 col-sm-4 col-md-2 col-lg-2'>
<div class='products " . $row['brandName'] . " all " . $row['product_range'] . "' id='products'>
<div class='hovereffect'>
<img class='img-responsive productimg' src='" . $row['img'] . "' alt=''>
<div class='overlay1'>
<h2> " . $row['name'] . "</h2>
<p>
" . $row['title'] . "
<br>
<br>
" . $row['price'] . "
<br>
<a href='remove_from_cart.php?name=" . $row['name'] . "&price=" . $row['price'] . "'>
Remove From Cart
</a>
</p>
</div>
</div>
</div>";
}
?>
I have printed the query to make sure the result of the implode is correct and it seems to be as i can run the result in phpmyadmin and it works fine.
Any help would be appreciated.
Try the following (NOT tested as far as I have not sufficent data provided form you):
UPDATED:
<?php
$sql = "SELECT * FROM `product_packs` WHERE `name` IN('" . implode("', '", $_SESSION['cart_items']) . "')";
// dump the query send to the database
$var_dump($sql);
$result = $conn->query($sql);
if ($result->num_rows > 0){
$resultset_count = $result->num_rows;
// dump the number of resultsets in the query
var_dump($resultset_count);
while($row = $result->fetch_assoc()){
echo "<div class='col-xs-6 col-sm-4 col-md-2 col-lg-2'>
<div class='products " . $row['brandName'] . " all " . $row['product_range'] . "' id='products'>
<div class='hovereffect'>
<img class='img-responsive productimg' src='" . $row['img'] . "' alt=''>
<div class='overlay1'>
<h2> " . $row['name'] . "</h2>
<p>
" . $row['title'] . "
<br>
<br>
" . $row['price'] . "
<br>
<a href='remove_from_cart.php?name=" . $row['name'] . "&price=" . $row['price'] . "'>Remove From Cart </a>
</p>
</div>
</div>
</div>
</div>";
}
?>
Why don't you use FIND_IN_SET? It will work for you, I don't know your database structure but I have created an example query that could be of help for you
why don't use FIND_IN_SET? it will work for you i don't know your database structure still i have created query for you it might help you
SELECT * FROM product_packs
WHERE (
FIND_IN_SET(cart_items, (SELECT cart_items TABLENAME
WHERE cart_items = '$_SESSION['cart_items']')
)
)
ORDER BY `product_id` ASC

Update a whole table with mysqli and php

I have created a page to list all users with their rights on the website. I used the code below to create and fill the table and it works great.
echo '<form method="post" id="detail" class="group" action="includes/setup_change.php?change=rights">';
echo '<div class="group">';
if (!empty($_GET['change'])) {
if ($_GET['change'] == 'rights') { echo '<span class="message_alert success"><span class="icon success"></span><span class="text">' . _('Your password was successfully changed') . '.</span></span>'; }
}
echo '<h2>' . _('Change') . ' ' . _('rights') . '</h2>';
// select database
mysqli_select_db( $mysqli, 'db_ccadmin' );
// check connection
if ( $mysqli->connect_errno > 0 ) {
trigger_error( _('Database connection failed') . ': ' . $mysqli->connect_error, E_USER_ERROR );
}
// sql query
$sql = "SELECT * FROM users";
$res = $mysqli->query( $sql );
if( !$res ) {
trigger_error( _('Wrong') . ' SQL: [' . $sql . ']. ' . _('Error') . ' : [' . $mysqli->error . ']' );
} else {
echo '<table id="table_sort_no_search">';
echo '<thead><tr>
<th class="username">' . _('Username') . '</th>
<th class="readonly">' . _('Read-only') . '</th>
<th class="manage">' . _('Manage') . '</th>
<th class="admin">' . _('Admin') . '</th>
</tr></thead>';
echo '<tbody>';
// output query results
while($row = $res->fetch_assoc()) {
echo '<tr>';
echo '<td><input type="text" name="username" value="' . $row['username'] . '" readonly></td>';
echo '<td><label><input type="radio" class="rights" name="rights_' . $row['username'] . '" value="1" ' . (isset($row['rights']) ? (($row['rights'] == '1') ? 'checked="checked"' : '' ) : '') . '></label></td>';
echo '<td><label><input type="radio" class="rights" name="rights_' . $row['username'] . '" value="2" ' . (isset($row['rights']) ? (($row['rights'] == '2') ? 'checked="checked"' : '' ) : '') . '></label></td>';
echo '<td><label><input type="radio" class="rights" name="rights_' . $row['username'] . '" value="3" ' . (isset($row['rights']) ? (($row['rights'] == '3') ? 'checked="checked"' : '' ) : '') . '></label></td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
// free results to free up some system resources
$res->free();
The trouble is updating the whole table in the database. I don't know how to do this. Is it even possible to update the whole table (as generated) at once? If yes, how?
What would be the code that I need to put in my setup_change.php?
It would be great if you could help me!
It is not clear what you want to update, but this is how you can update (example):
$mysqli->query("UPDATE birthday SET Manage = null");
Of course, this does not solve your problem, as there is infinite ways to update the whole table. What table do you want to update, what values do you want to set?

Eliminating space between table data

I am trying to get rid of the border space between the cells. I thought cellspacing would do the trick but there is still a slim white border. Can anyone give me some advice...
<body>
<center><table style="cellspacing:0; width:400px; border:none;">
<?
echo "<tr><td> Team </td><td>Correct Picks</td><td>Points</td></tr>";
while($row = mysql_fetch_array($memberslist)) {
if ($row['User_ID'] == $id) {
echo "<tr bgcolor=\"gray\"><td>" . $row['User_ID'] . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
} else {
echo "<tr><td>" . $row['User_ID'] . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
}
}
?>
</table></center>
</body>
cellspacing=0;
is no CSS. That was once a HTML attribute:
<table cellspacing="0" style="width:400px">
See as well the related (duplicate?) question:
How to set cellpadding & cellspacing in CSS?

php pass parameter issue

This my code:
<?php
$lijstDoelmannen = mysql_query("SELECT * FROM Speler WHERE positie = 'Doelman' ORDER BY familienaam, voornaam");
$teller = 1;
while($rij = mysql_fetch_array($lijstDoelmannen))
{
if($teller < 5){
echo "<td><a href='spelerDetail.php?spelerId='" . $rij['id'] . "><img src='images/spelers/unknown.png' alt='' width='50' />
<br /><br />" . $rij["id"] . " " . $rij['familienaam'] . " " . $rij['voornaam'] . "</a></td>";
}
}
?>
The problem is that in the hyperlink the parameter spelerId = spaces (not filled in). If I echo $rij["id"], it gives me the right value.
You have a ' in the wrong spot in your href.
"...<a href='spelerDetail.php?spelerId='" . $rij['id'] . ">..."
This should be:
"...<a href='spelerDetail.php?spelerId=" . $rij['id'] . "'>..."
<a href='spelerDetail.php?spelerId='" . $rij['id'] . ">
You need to move the apostrophe:
<a href='spelerDetail.php?spelerId=" . $rij['id'] . "'>
It's currently ending the link, before the variable is added.
You can also do:
echo "<td><a href='spelerDetail.php?spelerId={$rij['id']}'
while($rij = mysql_fetch_array($lijstDoelmannen))
{
if($teller < 5){
echo "<td><a href='spelerDetail.php?spelerId='" . $rij['id'] . "><img src='images/spelers/unknown.png' alt='' width='50' />
<br /><br />" . $rij["id"] . " " . $rij['familienaam'] . " " . $rij['voornaam'] . "</a></td>";
}
}
?>
I prefer writing the above code this way to avid these types of issues:
while($rij = mysql_fetch_array($lijstDoelmannen)){
if($teller < 5){ ?>
<td><a href="spelerDetail.php?spelerId=<?php echo $rij['id'] ?>">
<img src="images/spelers/unknown.png" alt="" width="50" />
<br /><br /><?php echo $rij['id'] . " " . $rij['familienaam'] . " " . $rij['voornaam'] ?></a></td>
<?php }} ?>

Categories