Use output from MySQL to extract additional info - php

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

Related

Increament id of td by Loop and getting id in function

I'm populating a table with database data, i want to get value of td that is row1, to perform action in PHP function, I dont know how to do this,
<?php
$sql = "SELECT DISTINCT name,c_no,email,speciality FROM doctor_temp";
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($result))
{?>
<?php $count=1;
$count++;?>
<tr>
<td><?php echo $row[0];?></td>
<td id=<?php echo $count;?>><?php echo $row[1];?></td>
<td><?php echo $row[2];?></td>
<td><?php echo $row[3];?></td>
<td><button type="button" class="btn btn-success" onclick="insert()">register</button></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<script type="text/javascript">
function insert()
{
var a = document.getElementById($count).innerHTML;
alert(a)
}
</script>
</body>
Watch out! Your code is wrong because you need to put the ID number instead of the PHP variable. If you want to use count you have to put it inside a PHP tag and echo it, but you are mixing the JS with the PHP code.
getElementById(<?php echo $count; ?>).innerHTML
Just look at the console log when you are debugging, please, that makes things a lot easier and it saves you from asking questions that you can answer yourself.
I don’t recommend that you preprocess JS however, that is an extremely bad practise for any programming language.

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

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.

PHP/jQuery - jQuery Plugin + PHP Foreach Returning Only One Working Result? | X-Editable For Twitter Bootstrap

There is a plugin called X-Editable where I it integrates into Bootstrap to have click to change Ajax requests. I have it all set up, and have it set to go, but my issue is that I have it in a foreach loop to echo out my users from my database, but it is only making the first row editable. screen
Here is my PHP Foreach,
<?php foreach($rows as $row): $userid = $row['id']; ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo htmlentities($row['username']); ?></td>
<td><?php echo htmlentities($row['email']); ?> </td>
<td><?php echo preg_replace('/([0-9]+\.[0-9]+\.[0-9]+)\.[0-9]+/', '\\1.**',$row['ip']); ?></td>
<td><?php echo htmlentities($row['timestamp'); ?></td>
<?php endforeach; ?>
Here is how I make the id "Username" editable,
<script type="text/javascript">
$('#username').editable();
</script>
The documentation for the plugin can be found here
Sorry if I show a lack of information, but I figure this is a pretty general question, why is the plugin only making the first result editable?
ID's must be unique, change id="username" to class="username", then do
$('.username').editable();
or do
<?php echo "username-".$userid; ?>
and then
$('[id^=username]').editable();

delete items that are checked without $_POST php

I've stuck with the following issue:
I have a table with texts and each one has a checkbox next to it. I also have a button-like link "Delete selected". What I want is to give the ability to the user to select many texts through checkboxes and delete the selected all together. The problem is tha I don't want to use a form so I can't use $_POST so I suppose I have to use $_SESSION. I have used sessions before but I don't know how to combine them with checkboxes..
if you could give me some tips or examples I would be very gratefull..
parts of my code to give you a clue..
view_texts.php
<?php
$sql="SELECT * FROM texts";
$res=mysql_query($sql) or die(mysql_error());
$i=1;
while($row=mysql_fetch_array($res))
{
?>
<tr>
<td><input type="checkbox" name="check[]" value="<?php echo $row['id']?>"/></td>
<td><?php echo $i; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo substr($row['description'],0,20); ?></td>
<td><?php echo $row['page_id']; ?></td>
<td><img src="images/user_edit.png" alt="" title="" border="0" /> </td>
<td><a href="delete_text.php?id=<?php echo $row['id'];?>" class="ask"><img src="images/trash.png" alt=""
title="" border="0" /></a></td>
</tr>
<?php
$i++;
}
?>
<span class="bt_red_lft"></span><strong>Διαγραφή επιλεγμένων</strong><span class="bt_red_r"></span>
delete_selected_texts.php
<?php
if (isset($_POST['check']))
{
foreach ($_POST['check'] as $value)
{
$sql = "DELETE FROM texts WHERE id =".$value;
mysql_query ($sql);
}
}
?>
this is the code that i use for textboxes inside a form tag.
If you're stuck on not using a form, for whatever reason, you're looking at using javascript and sending an AJAX request on the POST instead. I'd recommend using jQuery, as that greatly simplifies the process of selecting the elements you want to target.
It would look something like this...
$(document).function(){
$(":checkbox").click(function(e){
var deleteItem = $("input:id").val();
$.ajax({
type: "POST";
data: deleteItem;
url: "path/to/a/file.php";
success: function(data){
if (data="success"){
$(this).css("backgroundcolor", "blue");
} else {
alert("failed to find item");
}
}
});
}
You would need then to have a file that the Ajax request sends the data to that will build a list of items to delete/take items off that list, which will be stored and called up when the delete_selected_texts.php is called up to be deleted.
NB.: The jQuery above hasn't been checked. There's an excellent chance of there being several errors in there.

Form Submit button doesnt work with PHP loop

I have this code
<tbody>
<form action="dsf.php" method="post">
<?PHP if(mysql_num_rows($leerdb) > 0) {while ($rs = mysql_fetch_row($leerdb)) {?>
<tr>
<td><input name="idecod[]" type="checkbox" value="<?php echo $rs[0]; ?>" /></td>
<td><?php echo $rs[0]; ?></td>
<td><?php echo $rs[1]; ?></td>
<td><?php echo $rs[2]; ?></td>
<td><?php echo $rs[3]; ?></td>
<td><?php echo $rs[4]; ?></td>
<td><?php echo $rs[5]; ?></td>
<td>BsF. <?php echo $rs[6]; ?></td>
</tr>
<?PHP }}?>
<input class="enviar" type="submit" name="enviar" id="enviar" value="Editar Asesor" />
</form>
</tbody>
I do not know why the submit button doesnt work. When I click on it, nothing happend.
It seems that something in the php loop is making the mess.. But as I know, PHP goes first than HTML, so, when FORM HTML comes, PHP code was already executed.
Where is my error.??
Thanks in advance.
Roberto
You are generating invalid HTML.
You can't have a form wrapped around table rows without wrapping it around the entire table.
You can't have a submit button placed between table rows.
The browser you are using is likely trying to recover from the error in such a way that the form is moved somewhere where it is allowed, but where it doesn't contain any of the controls.
NB: Different browsers recover from having forms in inappropriate parts of tables in different ways.
Use a validator, and write real HTML.
instead of opening the brackets for the while and if, use the php short tags. Example:
<?php if (condition): ?>
output your html here
<?php endif; ?>
Same thing goes for while. read about it here: www.php.net/manual/en/control-structures.while.php

Categories