How to get checkboxes remained checked after being checked in another device - php

I am facing some issues trying to get checkboxes to stay checked in another device, I am able to get check boxes stay checked within the browser, so after the browser refreshed, the checkboxes remained checked, so is there any idea how i can do this?
so here is my code, firstly i included the dcConnect.php to connect to the database then i retrieve the data from the database to be displayed on a website
<?php
include_once("dcConnect.php");
$dcData = "SELECT dcID, dcServerName, dcServerAge, dcServerGender, dcServerMarital FROM dcUsers";
$result = $link->query($dcData);
if($result->num_rows >0){
echo"<table><tr><th></th><th>ID</th><th>Name</th><th>Age Group</th><th>Gender</th><th>Marital Status</th></tr>";
while($row = $result->fetch_assoc()){
echo"<tr><td><input type='checkbox' id='". $row["dcID"] ."' name='". $row["dcID"] ."' value='off' ></input></td><td>". $row["dcID"] ."</td><td>". $row["dcServerName"] ."</td><td>". $row["dcServerAge"] ."</td><td>". $row["dcServerGender"] ."</td><td>". $row["dcServerMarital"] ."</td></tr>";
}
echo "</table>";
}else{
echo"no results";
}
$link->close();
?>
Here is my website where the check boxes can stay checked after refreshed but not in another device
http://forstoringdata.com/default.php

It looks like currently you are using cookies to save the state of the checkboxes. Cookies are stored on the clientside (i.e. the user's computer) and not the server side, which is why you are not seeing the checkbox state preserved across devices.
In order to save and retrieve the checkbox state in the database, you will need to add an additional column to your database table. Following your pattern above, this would probably be a boolean column named something lke 'dcChecked'.
Then when you are printing your inputs, you would want to do something like this:
<input type="checkbox" <?php if($row['dcChecked']) { print 'checked' } ?></input>
(This is simplified for clarity, you'll still want to include your other attributes for ID, name, etc)

<?php
include_once("dcConnect.php");
$dcData = "SELECT dcID, dcServerName, dcServerAge, dcServerGender, dcServerMarital, dcChecked FROM dcUsers";
$result = $link->query($dcData);
?>
<?php if($result->num_rows > 0): ?>
<table>
<tr>
<th></th><th>ID</th>
<th>Name</th>
<th>Age Group</th>
<th>Gender</th>
<th>Marital Status</th>
</tr>
<?php foreach($rows as $row): ?>
<tr>
<td><input type='checkbox' id="<?php print $row["dcID"]; ?> " name="<?php print $row["dcID"]?>" value='off' <?php if($row["dcChecked"]): ?>checked<?php endif;?>></input></td>
<td><?php print $row["dcID"]; ?></td>
<td><?php print $row["dcServerName"]; ?></td>
<td><?php print $row["dcServerAge"]; ?></td>
<td><?php print $row["dcServerGender"]; ?></td>
<td><?php print $row["dcServerMarital"]; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php else: ?>
No results
<?php endif; ?>
Here's what the syntax would look like in context. I also refactored this a bit to make it a bit more readable.

Related

binding specific data to the url

I'm trying to bind the id from a row to the href output. that is getting a url something?id=*** in order to use $_GET and bring the id on the next page.
I need to be the id on the same row that is clicked on a table I'm displaying.
If I try to bind it by stating href=" wahtever?id=<php echo $row['id'] ?> the id will return as empty. If I use a loop it works but give me all the id's on the table.
I tried different solutions I found on internet like stating echo '<td> <a href="****?id='.$row['id'].' </a></td>' or making a new selection using php code on the href link... nothing seems to work.
I'm confused, how can I make a link on a table that will include the id of the clicked row?
My code looks like this now:
<td bgcolor="#FAB1CA"><a href="view_topic.php?id=<?php $sql="SELECT * FROM forum_question ORDER BY id DESC";
$result = mysqli_query($link, $sql);
while($rows = mysqli_fetch_assoc($result){
echo $rows['ID'] ; ?>">
Just to make it clearer, it is a simple table displaying 4 columns with different data using a loop, the first column is the id and the second one would be the topic, where I trying to build the links.
It sounds like you have 5 columns in a database table and you want to show them on the page, and link the topic cell to the topic page and pass the id of that topic.
I cleaned your code up a little and gave an example of how to do that. Keep in mind, I'm using an associative array so you'll need to be sure it matches what the columns are called in your database.
<table>
<tr>
<th>ID</th>
<th>Topic</th>
<th>Answers</th>
<th>Views</th>
<th>Date</th>
</tr>
<?php
$sql="SELECT * FROM forum_question ORDER BY id DESC";
$result = mysqli_query($link, $sql);
while($row = myslqi_fetch_assoc($result)) : ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td bgcolor="#FAB1CA">
<a href="view_topic.php?id=<?php echo $row['id']; ?>">
<?php echo $row['topic']; ?>
</a>
</td>
<td><?php echo $row['answers']; ?></td>
<td><?php echo $row['views']; ?></td>
<td><?php echo $row['theDate']; ?></td>
</tr>
<?php endwhile; ?>
</table>
I cant see $row variable in your code
you can use myslqi_fetch_assoc for get $row variable
i think this answer its true
<?php
$sql="SELECT * FROM forum_question ORDER BY id DESC";
$result = mysqli_query($link, $sql);
while($row = mysqli_fetch_assoc($result)){
?>
<td bgcolor="#FAB1CA"><a href="view_topic.php?id=<?php echo $row['id']?>go to view_topic</a></td>
<?php
}
?>

I have a record Id that needs to be selected to be updated

I have a simple issue I cannot figure out. I am trying to get the id of the record setup as a link to go to a second page that updates the record. I have the update working when I click on update it takes me to the record. I want the id to do the same.
<html>
<?php
require_once("../db_connect.php");
$stmt = $db->prepare("SELECT * FROM Users");
$stmt->execute();
?>
<?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { ?>
<table bgcolor=F2F2F2 width=1080 border='2'table-layout: fixed >
<br>
<tr>
<th>Id</th>
<th>Update</th>
<th>First Name</th>
<th>Last name</th>
<th>Address</th>
<th>Bio</th>
</tr>
<tr>
<?php echo "<td>
<a href='../update.php?id=" . $row['id'] . "'>ID</a></td>"?>
<?php echo "<td>
<a href='../update.php?id=" . $row['id'] . "'>Update</a></td>"?>
<td><?php echo $row['First Name']; ?></td>
<td><?php echo $row['Last Name']; ?></td>
<td><?php echo $row['Address']; ?></td>
<td><?php echo $row['Bio']; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
In general, it is a good practice to put duplicated content into a function or variable and then call it when needed, to improve code readability & to save time/space.
I have also noticed many people struggling with new syntax so I have split the "one-liners" and left comments explaining how does new syntax works.
function col_gen($slug,$id=''){
return (!empty($id))? // If ID parameter exist and not empty generate column with a link
'<td>'.$slug.'</td>': //else
'<td>'.$slug.'</td>';
}
And then, in your case, you can run this function inside a loop:
....
foreach($row as $k=>$slug){
echo ($k==='id')? //if key equals "id"
col_gen($slug,$slug) // Output column for ID
.col_gen('Update',$slug) // Output column for keyword Update
:col_gen($slug); //else output just column with the slug
/**
* Learn one-line PHP, you will love it, once you understand it...
* Full Example Above:
* echo ($k==='id') ? col_gen($slug,$slug).col_gen('Update',$slug):col_gen($slug);
**/
}

how to parse the contents of a particular row of a html table into array

I need to pass a one of these row contents of features-table to an array. But the problem is I used an iframe to display the table. And I need it to happen when after clicking the image of particular row.
In main.php
<iframe id="ifrm" src="reserved_tables/reserved_rooms_table.php"></iframe>
In reserved_rooms_table.php
<?php
include '../core/init.php';
$username = $user_data['username'];
$que = "SELECT * FROM ordered_rooms WHERE `username` LIKE '$username'";
$reslt = mysql_query($que);
echo '<table class="features-table" >';
echo '<thead>
<tr>
<td></td>
<td>Date</td>
<td>Start At</td>
<td>End At</td>
<td>Action</td>
</tr>
</thead>
<tbody>';
while($row = mysql_fetch_array($reslt)){
echo '<tr><td>' . $row['room_name'] .' </td><td> '. $row['date'] . '</td><td>'. $row['s_at'].'hrs</td>
<td>'. $row['e_at'].'hrs</td><td><input type="image" src="../images/cross.png" onclick="delete();" value=""></td></tr>';
}
echo "</tbody></table>";
mysql_close(); //connection closed
?>
It isn't entirely clear what you're trying to do. If you want to take data from an HTML table row and send it to a PHP array after a click then you'll need to use javascript.
Keep it simple. Create a link that sends the data to whatever URL/file does the processing:
while ($row = mysql_fetch_array($reslt)) {
?>
<tr>
<td><?php echo $row['room_name'] ?></td>
<td><?php echo $row['date'] ?></td>
<td><?php echo $row['s_at'] ?>hrs</td>
<td><?php echo $row['e_at'] ?>hrs</td>
<td><a id="my_link" href="some_file.php?room_name=<?php echo $row['room_name'] ?>&date=<?php echo $row['date'] ?>&s_at=<?php echo $row['s_at'] ?>&e_at=<?php echo $row['e_at'] ?>"><input type="image" src="../images/cross.png" value=""><a/></td>
</tr>
<?php
}
The data will then be available to some_file.php in $_GET.
Note that you can still use javascript to process this dynamically. You could add a click function to the link, so that when it is clicked, it does an AJAX call using the href value. For example:
$('#my_link').click(function() {
$.ajax({
url: this.href
});
});

Use output from MySQL to extract additional info

How can I use the output of a MySQL query as to extract extra info from the database about that output?
For example, if my query generates a list of names and there is extra info about each name existing in the database, when I click on the name on the output page, the system will extract the relevant info from the database and show it on another page.
EDIT: Here's the code:
<?php
// you can delete mysql-assoc
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
?>
<tr align="center">
<td width="5%">
<a href="// I don't know what do put here " >
<?php print("{$row["pid"]}");?>
</a>
</td>
<td><?php print("{$row["place"]}");?></td>
<td><?php print("{$row["date"]}");?></td>
<td><?php print("{$row["ppp"]}");?></td>
<td><input type="button" value="Book"></td>
</tr> <?php } ?>
Set each row of the first output results equal to a variable, and then have an if statement in php that looks for strings in those variables, and then (upon some sort of a page refresh button since php is server-side code) displays the additional content.
Great; here's what you're missing:
<a href="<?php // I don't know what to put here ?>" >
Should be:
<a href="<?php echo 'database.php?action=viewrecord({$row["pid"]})'?>" >
From there, you should be able to extract the relevant info using the pid of the user.
On a side note, unless you're really worried about spacing, you should just dump out the relevant HTML; doing so will save you the task of repeatedly typing <?php ?> every time you want to dump out a variable.
<?php
// you can delete mysql-assoc
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
echo "<tr align='center'>";
echo "<td width='5%'>";
echo "<a href='database.php?action=viewrecord({$row['pid']})'>";
echo "{$row['pid']}";
echo "</a>";
echo "</td>";
echo "<td>{$row['place']}</td>";
echo "<td>{$row['date']}</td>";
echo "<td>{$row['ppp']}</td>";
echo "<td><input type='button' value='Book'></td>";
echo "</tr>";
}
?>
Thanks for the help but this doesn't work :( I'm beginner in php when I press the record the url looks like localhost/ass/database.php?action=viewrecord%28{$row[
This isnt a direct answer to your question perse but since youre new to php im goign to through it out there. The syntax you uisng makes unicorns cry. It give me a headache jsut tryign to decipher it. Heres the same code in an alternative way:
<?php while($row = mysql_fetch_array($rs, MYSQL_ASSOC)): ?>
<tr align="center">
<td><?php printf('%s', url_encode('viewrecord('.$row['pid'].')'), $row['pid']); ?></td>
<td><?php echo $row['place']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['ppp']; ?></td>
<td><input type="button" value="Book"></td>
</tr>
<?php endwhile; ?>
However this database.php?action=viewrecord() worries me. I hope youre not using eval to execute a function passed by URL. Typically this would look something more like: database.php?action=viewrecord&id=2. You would verify that the action parameter is valid, and then invoke a function tied to that parameter which may or may not be the actual function name, and you would pass in the id parameter.
Eaxample database.php:
$action = isset($_POST['action']) ? $_POST['action'] : 'index';
switch($action) {
case 'viewrecord':
if(isset($_POST['id']) {
viewrecord((integer) $_POST['id']);
}
break;
// a whole bunch of other cases here for other actions
default:
// the default thing to do if an action is in valid
}
// the rest of your processing

PHP Repeating Region and Checkboxes

--Edited for clarity.
Database:
tblModule, contains a list of modules that can be enabled or disabled.
tblData, contains a list of trusts and the modules they have enabled. This links to tblModule on tblData.M01 = tblModule.mod_key
The PHP page is accessed from an index page and passes a variable lstModTrust to this page, to limit the returned records from tblData for a single trust. tblData.trust_key
A query runs, qryModuleList which returns a list of all modules. This is used to generate a table of all available modules. Each row shows module name tblModules.mod_name, module code tblModules.mod_code and a checkbox.
qryModData will return a list of the modules that are enabled for a single trust, and the corresponding checkboxes need to be ticked in the table.
This page will then be used to enable and disable modules for a trust. If a module is unticked the entry will be deleted from tblData, if it is ticked an entry will be inserted, and if no change then no change in the DB.
At the moment I'm having trouble getting the checkboxes ticked correctly based on qryModData
Any thoughts anyone?
--Edited to include code--
<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Module</td>
<td>Module Code</td>
<td> </td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_qryModuleList['mod_name']; ?></td>
<td><?php echo $row_qryModuleList['mod_code']; ?></td>
<td><input <?php if (!(strcmp($row_qryModData['M01'],$row_qryModuleList['mod_code']))) {echo "checked=\"checked\"";} ?>name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
</tr>
<?php } while ($row_qryModuleList = mysql_fetch_assoc($qryModuleList)); ?>
</table>
Then there's two SQL queries, one that generates the list to build the table, and the second which I'm trying to use to set the boxes to ticked.
qryModuleList
SELECT *
FROM tblmodules
ORDER BY mod_name ASC
qryModData
SELECT *
FROM tbldata
WHERE trust_key = varTrust
varTrust is pulled from a URL variable.
Apologies for not including the code in the first place.
--Edit for new code.
<?php while ($row_qryModuleList = mysql_fetch_assoc($qryModuleList)) { ?>
<tr>
<td><?php echo $row_qryModuleList['mod_name']; ?></td>
<td><?php echo $row_qryModuleList['mod_code']; ?></td>
<td><input <?php if (strcmp($row_qryModData['M01'],$row_qryModuleList['mod_key']) != 0) {echo "checked=\"checked\"";} ?>name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
</tr>
<?php } ; ?>
--Edited for new Code.
<tr class="tblHead">
<td>Module</td>
<td>Module Code</td>
<td>Enabled\Disabled</td>
</tr>
<?php
$mod_data = array();
while ($row_qryModData = mysql_fetch_assoc($qryModData))
array_push($mod_data, $row_qryModData['M01']);
$currentRow = 0;
while ($row_qryModuleList = mysql_fetch_assoc($qryModuleList)) {
?>
<tr bgcolor="<?php echo($currentRow++ % 2)?"#CCFFFF":"#FFCCFF";?>">
<td><?php echo $row_qryModuleList['mod_name']; ?></td>
<td><?php echo $row_qryModuleList['mod_code']; ?></td>
<td><input <?php if (false !== (array_search($mod_data, $row_qryModuleList['mod_key']))) echo "checked=\"checked\""; ?> name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
</tr>
<?php } ; ?>
I think I'm understanding this now. What you need to do is place all of the allowed modules into an array and use the array_search() function to find it.
Example:
$mod_data = array();
while ($row_qryModData = mysql_fetch_assoc($qryModData)) array_push($mod_data, $row_qryModData['M01']);
That will get all the available modules into one array.
Next, while you cycle through the 'ModuleList' query, use the array_search() method to try and find the 'ModKey' variable in it. If you do, check the box. If not, do nothing.
Example:
<td><input <? if (false !== (array_search($mod_data, $row_qryModuleList['mod_code'])) echo "checked=\"checked\" "; ?>name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
(The reason I use a "!==" is because the function can return false or something that might equal false and might not. Read the page I've linked above for more)

Categories