binding specific data to the url - php

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
}
?>

Related

Extra header at header row, HTML table

I explicitly inserted, inside a table row <tr>, 4 columns, but I am seeing an extra that is messing up with my table design. Added a image here, must say I am fetching data from a database, first time at this. This would be the php/html markup:
<form action="test.php" method="GET">
<?php
$query = "SELECT data0, data1, data2, data3 FROM table_name";
$result = mysqli_query($conn, $query);
echo "<table>";
echo "<tr>
<th>Info-1</th>
<th>Info-2</th>
<th>Info-3<th>
<th>Info-4</th>
</tr>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>{$row['data0']}</td>
<td>{$row['data1']}</td>
<td>{$row['data2']}</td>
<td>{$row['data3']}</td>
</tr>";
} ?>
Use PHP codes inside the PHP tags. Don't mess up with HTML tags with PHP. if you follow like this you can avoid such like mistakes
<?php
$query = "SELECT data0, data1, data2, data3 FROM table_name";
$result = mysqli_query($conn, $query); ?>
<table>
<tr>
<th>Info-1</th>
<th>Info-2</th>
<th>Info-3</th> // you are missing close tag here.
<th>Info-4</th>
</tr>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['data0']; ?></td>
<td><?php echo $row['data1']; ?></td>
<td><?php echo $row['data2']; ?></td>
<td><?php echo $row['data3']; ?></td>
</tr>
<?php } ?>

PHP Table - How to add different hyperlinks on each row of table which is extracted from database

Okay, my problem is that how can I set different hyperlinks to each row of a table in php?
so far i've done this -->
<?php
$conn = mysqli_connect("localhost", "root", "", "makaut_colleges");
$result = mysqli_query($conn, "SELECT * FROM collegelist");
while ($row = mysqli_fetch_assoc($result)):
?>
<tr>
<td><?php echo $row['College Name']; ?></td>
<td><?php echo $row['College Address']; ?></td>
<td><?php echo $row['College Code']; ?></td>
<td><?php echo $row['Review']; ?></td>
</tr>
<?php endwhile; ?>
How can i add different hyperlinks to each row which will be extracted from database? i'm using 'College Code' as primary key.
You can try something like this
Try this for making tr clickable
$dynamic_link = "dynamic.php";
<tr onclick="document.location = '<?php echo $dynamic_link ?>'">
td code---------
</tr>;
where $dynamic_link is dynamic based on your requirement
Thier should be some logic or linking with url either save in table or create some link

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.

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);
**/
}

Display own data only for users in PHP

I currently have made a page that shows all the users, but I just want to only display the currently logged in user, Please help me im begging you I spent 4 hours of nothing T_T im new at php and im very lost, im so hopeless right now
<?PHP
$customerquery=mysql_query("select * from customerinfo");
while($customerrows=mysql_fetch_array($customerquery)){
?>
<tr>
<td>Id</td><td>First Name</td><td>Last Name</td><td>Address</td><td>Contact No</td> <td>Username</td><td>Password</td><td>Edit</td>
</tr>
<tr>
<td><?PHP echo $customerrows['id'];?></td>
<td><?PHP echo $customerrows['fname'];?></td>
<td><?PHP echo $customerrows['lname'];?></td>
<td><?PHP echo $customerrows['address'];?></td>
<td><?PHP echo $customerrows['contactno'];?></td>
<td><?PHP echo $customerrows['username'];?></td>
<td><?PHP echo $customerrows['password'];?></td>
<td>edit</td>
</tr>
<?PHP } ?>
You need to specify a WHERE condition in your query. That will fetch only one row, so you will not need a while loop:
$id = (int) $_GET['id']; // assuming you pass user id in URL
$customerquery = mysql_query("select * from customerinfo WHERE id = $id");
$customerrows = mysql_fetch_array($customerquery);
// rest of your html

Categories