I am selecting user_id from table follow and then calling data from other table members matching that user_id but when I echo out It shows only one member with least user_id. So I thought displaying members in table may help and now my code is like this:
$following =$db->prepare("SELECT user_id FROM follow WHERE uid_fk=:userid order by user_id");
$following->execute(array(':userid'=>$userid));
$row = $following->fetch(PDO::FETCH_ASSOC);
$user_id = $row['user_id'];
$check =$db->prepare("SELECT * FROM members WHERE userid=:user_id");
$check->execute(array(':user_id'=>$user_id));
$row = $check->fetch(PDO::FETCH_ASSOC);
<table>
<tr>
<?php do { ?>
<td>
<div><p><?php echo $row['id']; ?></p></div>
<div><p><?php echo $row['name']; ?></p></div>
<div><p><?php echo $row['about']; ?></p></div>
<div style="height:20px;"></div>
</td>
<?php
$row = $following->fetch(PDO::FETCH_ASSOC);
if (!isset($nested_List)) {
$nested_List= 1;
}
if (isset($row) && is_array($row) && $nested_List++%2==0) {
echo "</tr><tr>";
}
} while ($row);
?>
</table>
As you can see my table is only for $following and so now it displays all members $user_id in table but how to add second query to table code i.e, $check so that I can also echo out info about members in table. I don't want to store member info in follow table any other way to do that?
Actually I was omitting where attribute so this is how we could call data from two tables and show the result in table.
$sql = "SELECT * FROM members INNER JOIN follow ON follow.user_id=members.userid where user_id=$userid ORDER BY userid "; $query = $db->prepare($sql);
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);
<table>
<tr>
<?php do { ?>
<td>
<div><p><?php echo $row['id']; ?></p></div>
<div><p><?php echo $row['name']; ?></p></div>
<div><p><?php echo $row['about']; ?></p></div>
<div style="height:20px;"></div>
</td>
<?php
$row = $following->fetch(PDO::FETCH_ASSOC);
if (!isset($nested_List)) {
$nested_List= 1;
}
if (isset($row) && is_array($row) && $nested_List++%2==0) {
echo "</tr><tr>";
}
} while ($row);
?>
</table>
Related
I have a field called 'availability' that can be changed between 'Available' and 'UNavailable' for users to choose. If the user is available, I want to have a background of a green color; red is for UNavailable users.
$sql = $db_con->prepare("SELECT * FROM `users` WHERE username = '" . $_SESSION['username'] . "'");
$sql->execute();
while($row = $sql->fetch()){
?>
<tr>
<td>
<?php if ($availability = 'UNavailable') { ?>
<a style="margin:10px;background-color:#FF2828;color:#fff;border-radius:25px;padding:10px;" href="avail/index.php" class="true_home"><?php echo $row['availability']; ?></a></td>
<?php
} else if ($availability = 'Available') { ?>
<a style="margin:10px;background-color:#008000;border-radius:25px;padding:10px;" href="avail/index.php" class="true_home"><?php echo $row['availability']; ?></a></td>
<?php
}
}
?>
Whichever availability status I list first, the color always chooses that one. It ignores the else. I've used else if and just else to see if any changes would occur to allow the value to dictate the background color. Neither have worked.
As always, I truly value everyone's input.
i think this will help you
<style>
.Available {
background-color:#008000;
}
.UNavailable {
background-color:#FF2828;
}
</style>
<?php
$sql = $db_con->prepare("SELECT * FROM `users` WHERE username = '" .
$_SESSION['username'] . "'");
$sql->execute();
while ($row = $sql->fetch())
{ ?>
<tr>
<td>
<a style="margin:10px;border-radius:25px;padding:10px;" href="avail/index.php" class="true_home <?php echo $availability ?>"><?php echo $row['availability']; ?></a>
</td>
</tr><?php
}
?>
I have pagination for a table to display data from the database in the table. This was working fine and I tried to add in a button which only admin's can see. If the user is not an admin they will not see this button. This feature works but once I did it, the pagination only shows one row of data compared to the maximum of 10 per page.
This is my code:
public function dataview($query)
{
$stmt = $this->db->prepare($query);
$stmt->execute();
if($stmt->rowCount()>0) // display records if there are records to display
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
$poll_id = $row['poll_id'];
$question = $row['question'];
?>
<tr>
<td><?php echo $row['poll_id']; ?></td>
<td><?php echo $row['question']; ?></td>
<td>Open Poll</td>
<td>Results</td>
<?php
$stmt = $this->db->prepare("SELECT * FROM users WHERE user_id=:user_id");
$stmt->execute(array(':user_id'=>$_SESSION['user_session']));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() > 0){
$admin = $userRow['admin'];
if($admin == 1){
?>
<td>Delete Poll</td>
<?php
?>
</tr>
<?php
}
}
}
}
else
{
?>
<tr>
<td>Nothing here...</td>
</tr>
<?php
}
}
and this is the code on the html page which uses the pagination
<table align="center" border="1" width="100%" height="100%" id="data">
<?php
$query = "SELECT * FROM polls";
$records_per_page=10;
$newquery = $paginate->paging($query,$records_per_page);
$paginate->dataview($newquery);
$paginate->paginglink($query,$records_per_page);
?>
</table>
You are reusing variables like $stmt inside the loop that uses it. So do this:
$stmt = $this->db->prepare($query);
$stmt->execute();
if($stmt->rowCount()>0) // display records if there are records to display
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
$poll_id = $row['poll_id'];
$question = $row['question'];
?>
<tr>
<td><?php echo $row['poll_id']; ?></td>
<td><?php echo $row['question']; ?></td>
<td>Open Poll</td>
<td>Results</td>
<?php
$stmt2 = $this->db->prepare("SELECT * FROM users WHERE user_id=:user_id");
$stmt2->execute(array(':user_id'=>$_SESSION['user_session']));
$userRow=$stmt2->fetch(PDO::FETCH_ASSOC);
if($stmt2->rowCount() > 0){
$admin = $userRow['admin'];
if($admin == 1){
?>
<td>Delete Poll</td>
<?php
?>
</tr>
<?php
}
}
}
}
else
{
?>
<tr>
<td>Nothing here...</td>
</tr>
<?php
}
I have replace the $stmt inside the loop by $stmt2.
I am trying to echo $classname only once.
So it shows like this for example.
Puppy Dog
1st blah blah
2nd whoever
3rd extra
at present it shows like:
Puppy Dog
1st blah blah
Puppy Dog
2nd whoever
Puppy Dog
3rd extra
<?php
// SO UPDATE THE QUERY TO ONLY PULL THAT SHOW'S DOGS
$query = "SELECT c.* , p.* FROM result c,dogs p WHERE c.dog_id=p.dog_id";
$result = mysqli_query($connection, $query) or trigger_error
("Query Failed! SQL: $query - Error: ". mysqli_error
($connection), E_USER_ERROR);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$placement = $row['placement'];
$classname = $row['class_name'];
$dog_name = $row['dog_name'];
$award = $row['award'];
?>
<table>
<tr>
<td><strong><?php echo $classname ?></strong> </td><br>
</tr>
<tr>
<td><strong><?php echo $placement, $award ?></strong> <?php echo $dog_name ?></td>
</tr>
</table>
<?php }}} ?>
Use counter to check if it is already displayed:
$ctr = 0;
while{
$classname = $row['class_name'];
if($ctr == 0){
echo $classname;
$ctr++;
}
//display the rest
...
}
So, don't know why are you using <table> inside the while loop, this will print as per your no's of rows.
Here is the basic example, you can store the values in an array than use it with your HTML:
Example:
<?php
if ($result) {
$myarr = array();
while ($row = mysqli_fetch_assoc($result)) {
$myarr[$row['class_name']][] = $row; //store values into an array against each class in group
}
}
foreach ($myarr as $key => $value) {
echo "Class Name: ". $key."<br/>"; // will print class name
foreach ($value as $fvalue) {
echo "Placement: ".$row['placement']."<br/>";; // placement
echo "Dog Name: ".$row['dog_name']."<br/>"; // dog name
echo "Award: ".$row['award']."<br/>";; // award
}
}
?>
Other solution is using incremental variable in while loop as mentioned in other answers.
You can keep track record by index in while loop like :
<?php
// SO UPDATE THE QUERY TO ONLY PULL THAT SHOW'S DOGS
$query = "SELECT c.* , p.* FROM result c,dogs p WHERE c.dog_id=p.dog_id";
$result = mysqli_query($connection, $query) or trigger_error
("Query Failed! SQL: $query - Error: ". mysqli_error
($connection), E_USER_ERROR);
if ($result) {
$i = 1;
while ($row = mysqli_fetch_assoc($result)) {
$placement = $row['placement'];
$classname = $row['class_name'];
$dog_name = $row['dog_name'];
$award = $row['award'];
?>
<table>
<tr>
<td><strong><?php if($i==1) { echo $classname; } ?></strong> </td><br>
</tr>
<tr>
<td><strong><?php echo $placement, $award ?></strong> <?php echo $dog_name ?></td>
</tr>
</table>
<?php $i++; }}} ?>
The Opening and Closing <Table> Tags should be outside your Loop if you expect to have just one Table. Even the <tr> Tags should encompass the <td> Tags if you wish to have rows containing 2 Cells like the Code below shows:
<?php
// SO UPDATE THE QUERY TO ONLY PULL THAT SHOW'S DOGS
$query = "SELECT c.* , p.* FROM result c,dogs p WHERE c.dog_id=p.dog_id";
$result = mysqli_query ($connection, $query) or trigger_error
("Query Failed! SQL: $query - Error: ".
mysqli_error($connection), E_USER_ERROR);
?>
<table>
<?php
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$placement = $row['placement'];
$classname = $row['class_name'];
$dog_name = $row['dog_name'];
$award = $row['award'];
?>
<tr>
<td>
<strong><?php echo $classname ?></strong>
<!-- DO YOU NEED THIS <BR>TAG HERE? <br> -->
</td>
<td>
<strong><?php echo $placement, $award ?></strong><?php echo $dog_name ?>
</td>
</tr>
<!-- EXCEPT IF YOU WISH TO HAVE ONE COLUMN, THE ROW BELOW IS UNNECESSARY -->
<!--
<tr>
<td>
<strong><?php echo $placement, $award ?></strong> <?php echo $dog_name ?>
</td>
</tr>
-->
<?php
} // CLOSE THE WHILE LOOP;
} // CLOSE THE IF STATMENT;
?>
</table>
I'm trying to make a simple blog to my site and there is no tutorial of how to make one properly, so i'm trying to make one from scratch. My problem right now is to display a template of posts in a loop with variables. Here are the columns of my table:
Table blog
id
user_id
date
title
content
Code of variables:
<?php
// query //
$sql = mysql_query("SELECT id, user_id, date, title, content FROM blog");
$row = mysql_fetch_array($sql);
// variables //
$user_id = $row['user_id']; //only the id, not the name
$date = $row['date'];
$title = $row['title'];
$content = $row['content'];
// get user_id name //
$sql2 = mysql_query("SELECT name FROM users WHERE id = '$user_id'");
$row2 = mysql_fetch_array($sql2);
$author = $row2['name']; //last variable
?>
What i'm trying to do, is to display them all descendant from id value from the next template vertically:
<div id="container">
<div align="center" style="background-color:gray"><b><?php echo $title;?></b></div>
<div align="right">Author: <?php echo $author;?> Date: <?php echo $date;?></div>
<div><?php echo $content;?></div>
</div>
<hr>
How could i loop the template with different variables values in a simple page?
try this:
<?php
// query //
$sql = mysql_query ( "
SELECT b.id, b.user_id, b.date, b.title, b.content, u.name
FROM blog b, users u
WHERE b.user_id = u.id
ORDER BY b.id DESC" );
while ( $row = mysql_fetch_array ( $sql ) ) {
// variables //
$user_id = $row ['user_id']; // only the id, not the name
$date = $row ['date'];
$title = $row ['title'];
$content = $row ['content'];
$author = $row ['name']; // last variable
?>
<div id="container">
<div align="center" style="background-color: gray">
<b><?php echo $title;?></b>
</div>
<div align="right">Author: <?php echo $author;?> Date: <?php echo $date;?></div>
<div><?php echo $content;?></div>
</div>
<hr>
<?php
}
?>
first make one query for info if you have a lot of rows it will eat resources
SELECT b.id, b.user_id, b.date, b.title, b.content, u.name
FROM blog b, users u
WHERE b.user_id = u.id
ORDER BY b.id DESC
then you need to loop the query using:
while ( $row = mysql_fetch_array ( $sql ) ) {}
First you need to put your blog table in an array:
$sql = mysql_query("SELECT id, user_id, date, title, content FROM blog");
while($row = mysql_fetch_array($sql)) {
$blog[] = $row;
}
Then, in the template:
<div id="container">
<div align="center" style="background-color:gray"><b><?php echo $title;?></b></div>
<div align="right">Author: <?php echo $author;?> Date: <?php echo $date;?></div>
<div><?php
foreach($blog as $val) {
echo "<h1>".$val['title']."</h1>"M
echo $val['content'];
}
?></div>
</div>
<hr>
$records = mysql_fetch_array($sql);
$blogs_table = '<table> <tr> <th>User id</th> <th>Date</th> <th>Title</th> <th>Content</th></tr>';
foreach($records as $row) {
$blogs_table .= '<tr>';
$blogs_table .= '<td>' . $row ['user_id'] .'</td>';
$blogs_table .= '<td>' . $row ['date'] .'</td>';
$blogs_table .= '<td>' . $row ['title'] .'</td>';
$blogs_table .= '<td>' . $row ['content'] .'</td>';
$blogs_table .= '</tr>';
}
$blogs_table .= '</table>';
At last you can print your $blogs_table table to any where like,
<div align="right"><?php echo $blogs_table ?></div>
I want to have the ability to show all the entries in a database table and by each one give the user the ability to delete specific ones.
I am currently using a for each loop that loops through the database showcasing each entry.
$result = mysql_query("SELECT * FROM KeepScores");
$fields_num = mysql_num_fields($result);
echo "<table><tr>";
// printing table headers
echo "<td>Recent Posts</td>";
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
How would to add a delete button that appears by each one and removes the entry from the database table?
You can do it with forms:
//main.php
<?php $result = mysql_query("SELECT * FROM KeepScores"); ?>
<table>
<tr>
<td>Recent Posts</td>
</tr>
<?php while($row = mysql_fetch_array($result)) : ?>
<tr>
<td><?php echo $row['field1']; ?></td>
<td><?php echo $row['field2']; ?></td>
<!-- and so on -->
<td>
<form action="delete.php" method="post">
<input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<?php endwhile; ?>
</table>
//delete.php:
<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
header('Location: main.php');
}
Or you can do it with jQuery and AJAX:
//main.php
<?php $result = mysql_query("SELECT * FROM KeepScores"); ?>
<table>
<tr>
<td>Recent Posts</td>
</tr>
<?php while($row = mysql_fetch_array($result)) : ?>
<tr id="<?php echo $row['id']; ?>">
<td><?php echo $row['field1']; ?></td>
<td><?php echo $row['field2']; ?></td>
<!-- and so on -->
<td>
<button class="del_btn" rel="<?php echo $row['id']; ?>">Delete</button>
</td>
</tr>
<?php endwhile; ?>
</table>
<script>
$(document).ready(function(){
$('.del_btn').click(function(){
var del_id = $(this).attr('rel');
$.post('delete.php', {delete_id:del_id}, function(data) {
if(data == 'true') {
$('#'+del_id).remove();
} else {
alert('Could not delete!');
}
});
});
});
</script>
//delete.php
<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
$result = mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
if($result !== false) {
echo 'true';
}
}
It's all untested and sure needs some adjustment for your specific project, but I think you get the idea and I hope it helps.
Next time, please post your schema if you ask stuff about database.
I thought I would improve on this a little bit by wrapping the delete post in a class and function. I was having the same problem. and this worked great for me. Thanks again # Quasdunk
<?php
// Class to hold the remove post function
class someClass{
//Function for removing the post
function removePost(){
if(isset($_POST['delete_id']) && (!empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
$result = mysql_query("DELETE FROM post WHERE post_id='".$delete_id."' AND post_member='" . $_SESSION['SESS_USER'] . "'");
if($result !== false) {
echo 'true';
}
}
}
}
if(isset($_SESSION['SESS_MEMBER_ID'])){
$member = $_SESSION['SESS_USER'];
$res = mysql_query("SELECT * FROM post WHERE post_member='$member' ORDER BY timestamp DESC") or die(mysql_error());
$i = new someClass;
while($row = mysql_fetch_array($res)){
echo '<div style="width:100%;margin:0 auto;border-top:thin solid #000;">';
echo '<div style="width:600px;margin:0 auto;padding:20px;">';
echo $row['post_text'] . '<br>';
$postID = $row['post_id'];
echo '<div style="border-top:thin solid #000;padding:10px;margin-top:5px;background-color:#CCC;">';
echo 'You posted this on: ' . $row['post_date'] . '#' . $row['post_time'];
echo '<div style="float:right;">
<form method="post" action="'. $i->removePost() .'">
<input type="hidden" name="delete_id" value="'.$row['post_id'].'" >
<input type="submit" value="Delete Post">
</form>
</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
}
?>
Produce a key to each table, using jquery,then link it to a php file which an accept the key and delete from the specific table (which also can be passed through jquery)