Getting the value from ($_GET["Value"]) - php

// PAGE ONE
This is the index page, Here I am printing out rows from a list of
movie´s with some basic info, title , year.
I am also adding edit and delete links to the movies,
these work by passing the row id's of that movie.
$sql = "SELECT c.* , d.* FROM category c , movies d WHERE c.ID=d.ID";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<div class='floatbox collection'><table><tr><th>Titel</th><th>regissör</th><th>År</th><th>Genre</th><th>Ändra</th><th>Ta bort</th>";
while($row = $result->fetch_assoc()) {
echo
"<tr><td>".$row["title"]."</td><td>".$row["director"]."</td><td>"
.$row["year"]."</td><td>".$row["category"]."</td>
<td><a href='edit.php?row=".$row["ID"]."'>justera</a></td>
// delete link to send ID to next page
<td><a href='delete.php?delete=".$row["ID"]."'>stryk</a></td>";
}
echo "</table></div>";
} else {
echo "0 results";
}
// PAGE TWO
Here I have my $row[ID] trough $_get by the link on the previous page,
I have checked the value by "dumping" so I know that the row ID is in that
variable: The thing is, How do I call that $ID in my sql statement?
I'm trying to delete by calling that ID on the table row.
// Variable with correct ID value
if(isset($_GET["ID"]))
if($_GET["ID"]) = $ID; // This doesn't work, Do I need to convert
the $get_ID to a variable with the ID that can be called in the statement?
or is my syntax wrong?
// Delete join
$sql = "SELECT * FROM movies, category
INNER JOIN category ON movies.ID = category.ID
DELETE WHERE movies.ID = '$ID'"; // No syntax works here
// Any help appreciated.
// konfirmering
if ($conn->query($sql) === TRUE) {
echo "Register struket <br>
<a href='panel.php'>Gå tillbaka</a>";
} else {
echo "Fel vid anslutning : " . $conn->error;
}

As per your code your delete.php would be like this:
// Variable with correct ID value
if (isset($_GET["delete"])) {
$ID = $_GET["delete"];
// Delete join
$sql = "DELETE FROM movies, category INNER JOIN category ON movies.ID = category.ID WHERE movies.ID = '$ID'";
// konfirmering
if ($conn->query($sql) === TRUE) {
echo "Register struket <br><a href='panel.php'>Gå tillbaka</a>";
} else {
echo "Fel vid anslutning : " . $conn->error;
}
}

Related

SQL how to get all images to show for respective units

My question is how to get multiple images to display next to their respective units information. I am able to print out all the images below but unable to get each listing to print out its respective images. originally i was trying to do it as a single query but it was only printing out the same image multiple times so i tried to do two seperate queries but it did not work, how can i fix it?
// Define the query but only for info:
$querystats = 'SELECT 8052monticello.UNIT, 8052monticello.SIZE, 8052monticello.id, 8052monticello.PRICE';
$querystats.= ' FROM 8052monticello';
if ($r = mysqli_query($connection, $querystats)) { // Run the query.
// Retrieve and print every record:
while ($row = mysqli_fetch_assoc($r)) {
if ($row['PRICE']){
// echo "<img src='images/".$row['image_name']."' width='100'>";
print "<p><h3>{$row['UNIT']} Unit #</h3>
{$row['SIZE']} Sq Feet<br>
{$row['PRICE']} Monthly rent<br>
Edit
Delete
</p><hr>\n";
}
}
}
//get the images
$queryimage = 'SELECT photos.image_name, photos.fk_unit, 8052monticello.UNIT';
$queryimage.= ' FROM photos, 8052monticello';
$queryimage.= ' WHERE photos.fk_unit= 8052monticello.unit';
if ($r = mysqli_query($connection, $queryimage)) { // Run the query.
// Retrieve and print every record:
while ($row = mysqli_fetch_assoc($r)) {
if ($row['image_name']){
echo "<img src='images/".$row['image_name']."' width='100'>";
}
}
}
You can use a JOIN in your query to join both the monticello and photos table:
$query = '
SELECT
m.UNIT, m.SIZE, m.id, m.PRICE,
p.image_name
FROM 8052monticello m
INNER JOIN photos p ON p.fk_unit = m.unit';
Now you have access to $row['image_name'] in your loop.
while ($row = mysqli_fetch_assoc($r)) {
if ($row['PRICE']){
print "<img src=\"images/{$row['image_name']}\" width='100'>
<p><h3>{$row['UNIT']} Unit #</h3>
{$row['SIZE']} Sq Feet<br>
{$row['PRICE']} Monthly rent<br>
Edit
Delete
</p><hr>\n";
}
}
Updated: For each row, images are fetched from the database.
$query = "SELECT
m.UNIT, m.SIZE, m.id, m.PRICE
FROM 8052monticello m ";
$res = mysqli_query($connection, $query);
if (!$res){
// throw error here
}
$query2 = "SELECT p.image_name
FROM photos p
WHERE p.fk_unit = '%s' ";
while ($row = mysqli_fetch_assoc($res)) {
$res2 = mysqli_query($connection, sprintf($query2, $row["UNIT"]));
while($img = mysqli_fetch_assoc($res2)) {
echo "<img src=\"images/{$img['image_name']}\" width='100'>";
}
print "<p><h3>{$row['UNIT']} Unit #</h3>
{$row['SIZE']} Sq Feet<br>
{$row['PRICE']} Monthly rent<br>
Edit
Delete
</p><hr>\n";
}

News comments count

I'm tired of searching for the solution my news comment system.
What i have.
I have 2 different mysql tables item (id, title, category_id, details) and comments (id, item_id, comment)
If i have single news then counting is fine like here in picture:
Single news
Code:
if (!empty($comments)) {
$comm = count($comments);
} else {
$comm = 0;
}
But if i use same code category view, result is:
Category view
If i use code:
$sqls = mysql_query("SELECT c.item_id,
COUNT(c.comment)
FROM comments c
RIGHT JOIN items i
ON c.item_id = i.id
GROUP BY c.item_id");
$comm = mysql_num_rows($sqls);
$smarty->assign('comm',$comm);
Result is :Some number of comments
How to make possible to see the Category View the correct number of comments?
TRY this example. The result is the same as the screenshot below but with some background colour to distinguish the difference. Of course you should change to your own connection and layout.
<?php
$db = new PDO('sqlite:news.db');//change to your own
$sql = $db->prepare("
SELECT item.id, item.title, item.details, comments.comment, COUNT(comments.id) AS NumberOfComment
FROM item LEFT JOIN comments ON item.id = comments.item_id
GROUP BY item.id ORDER BY item.id");
$sql->execute();
$row = $sql->fetchAll();
//get comment
$comment = $db->prepare("SELECT comment FROM comments WHERE item_id = ?");
foreach ($row as $rows){
echo "<br>";
echo "Title: ".$rows['title'];
echo "<br>";
echo "Details: ".$rows['details'];
echo "<br>";
echo "<i>".$rows['NumberOfComment']." comments </i>";
echo "<br>";
$comment->execute(array($rows['id']));
$comments = $comment->fetchAll();
echo '<div style="background-color:pink;">';
foreach ($comments as $showcomment){
echo $showcomment['comment'];
echo "<br>";
}
echo "</div>";
echo "<br>";
}
?>
example item table
example comment table
and the result is...

Show result of MYSQL select query in two div according to column name, using a single select query

I have a table with column names id, name, category, title,and news. Once select query executes I get all the required data from table but i need to split up this data into two div according to the category they belong. there are two different categories, say A and B.
I need to show all the data with a column category name A in one div and same with category B.
$sql= "SELECT id, title ,news ,category FROM news ";
$query=mysql_query($sql, $ex)or die(mysql_error()) ;
while($row=mysql_fetch_assoc($query)){
$title=$row['title'];
if($row['category']==' Latest News'){
echo $row['title'];
}
}
My code doesn't work. Any idea?
You can first separate out category wise data into separate arrays.
$cat1 = array();
$cat2 = array();
$sql= "SELECT id, title, news, category FROM news";
$query = mysql_query($sql, $ex) or die(mysql_error());
if(mysql_num_rows($query) > 0)
{
while($row = mysql_fetch_assoc($query))
{
if($row['category'] == 'A')
{
$cat['title'] = $row['title'];
$cat['news'] = $row['news'];
$cat1[] = $cat;
}
else
{
$cat['title'] = $row['title'];
$cat['news'] = $row['news'];
$cat2[] = $cat;
}
}
}
Then you can use foreach to print the data into separate div
if(count($cat1) > 0)
{
foreach($cat1 as $category1_data)
{
echo "<div class='cat1'>";
// data of category A
echo "</div>";
}
}
if(count($cat2) > 0)
{
foreach($cat2 as $category2_data)
{
echo "<div class='cat2'>";
// data of category B
echo "</div>";
}
}

Sql UPDATE on link click

I'm trying to update a one value when a user clicks on a link.
<?php
$data = mysql_query("SELECT * FROM comments a LEFT JOIN pins b ON a.pin_id = b.id INNER JOIN board c on b.board_id = c.id WHERE a.to_id = '$myid' AND a.status = 'unviewed'")
or die(mysql_error()); while($info = mysql_fetch_array( $data ))
{
Print "<li>";
Print "<a href='/board/pins/".$info['board_id']."/".$info['pin_id']."'>";
Print "<img src='".$info['pin_url']."' width='50' align='left'>";
Print "<font size='1'>comment received on ".$info['date']."</font></a>";
Print "Collection: ".$info['board_name']."</li>";
}
?>
The link is the one above.
how can I edit the above code to include an update query "UPDATE comments SET status='viewed' WHERE to_id = '$myid' AND id='$postid'" when the link is clicked?
EDIT:
this is my mark_viewed.php:
<?php
$con=mysqli_connect("XXX","XXX","XXX","XXX");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// get values sent from address bar
$myid=$_GET['myid'];
$postid=$_GET['postid'];
mysqli_query($con,"UPDATE comments SET status='viewed' WHERE to_id ='$myid' AND id='$postid'");
mysqli_close($con);
?>
And my the send page:
<?php // display of the notifications dropdown menu
$query_select = "SELECT * FROM comments a LEFT JOIN pins b ON a.pin_id = b.id LEFT JOIN board c on b.board_id = c.id WHERE a.to_id = '$myid' AND a.status = 'unviewed' ORDER BY a.date DESC";
$result_select = mysql_query($query_select) or die(mysql_error());
$rows = array();
while($row = mysql_fetch_array($result_select))
$rows[] = $row;
foreach($rows as $row){
$myid = $row['user_id']; // my id
$name = $row['board_name']; // collection name
$boardid = $row['board_id']; // collection id
$postid = $row['pin_id']; // post id
$url = $row['pin_url']; // image url
echo "<li><a href='/board/pins/$boardid/$postid' data-myid='$myid' data- postid='$postid' class='markviewed'>";
echo "<img src='$url' height='50' width='50'>";
echo "New comment in $name.";
echo "</a></li>";
}
?>
I have the JS in the head of the same page but it isn't working. The varibles are fine on this page $myid and $postid but they aren't being sent/received on mark_viewed.php
Try this code (AJAX as suggested in comments). It assumes What it basically does is:
Print relevant ids into html as attributes.
Binds a listener to click event on those links
After pinging http://domain.com/mark_viewed.php (file that does the query updating comments as viewed) it redirects the browser to the desired location (/board/pins/...)
//html part
<?php
$myid = 1234;
$postid = 333;
//echo prepared link with ids in it's data-attributes
echo '<a href="/board/pins/"'.$info['board_id'].'/'.$info['pin_id'].'"
data-myid="' . $myid . '"
data-postid="' . $postid . '"
class="markviewed">mark ids as viewed and go to /board/pins/...</a>';
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
//bind a listener to "click" event on links with class "markviewed"
$('a.markviewed').click(function(event) {
//prevent default behavior just in case
event.preventDefault();
//get ids from clicked <a>
var myid = $(this).attr('data-myid');
var postid = $(this).attr('data-postid');
//ping the address to mark clicked link as viewed
$.ajax('http://domain.com/mark_viewed.php?myid=' + myid + '&postid=' + postid);
//redirect to the link in the href attribute
window.location.href = $(this).attr('href');
});
});
</script>

loop problem within a statement

i hope someone can help about to scream!
basically I am trying to do a few things with the statement below;
First i want to check if the user id exists in member_categories_position.
If it Does i want then to exclude all entries from the second statement where member_id equals all results from the first statement
the third statement is the else statement that displays if the member_id is not present in the member_categories position.
PROBLEM - the result from the first system loops fine, however when i try and insert into the second statement (!='$memid') is produces no results and has no effect. I think the problem is that $memid is a looped result.
How do i get the second statement to say that any member_id that is in member_categories_position will not show in that statement?
$sql2 = "
SELECT *
FROM member_categories_position a
JOIN member_users b
ON b.id = a.member_id";
$rs2 = mysql_query($sql2);
while ($row = mysql_fetch_array($rs2))
{
$memid = "".$row['member_id']."";
}
if(mysql_num_rows($rs2) != 0)
{
$new= "
SELECT *
FROM member_categories
JOIN member_users
ON member_categories.member_id=member_users.id
JOIN member_config
ON member_categories.member_id=member_config.member_id
WHERE
member_categories.categories='$category'
AND member_categories.member_id !='$field'
GROUP BY member_config.member_id
ORDER BY RAND() limit 0,42";
$rs = mysql_query($new);
while ($row = mysql_fetch_assoc($rs))
{
echo "result excluding member ids from the first statement";
}
echo "<div class=\"clear\"></div>";
}
else
{
$new= "
SELECT *
FROM member_categories
JOIN member_users
ON member_categories.member_id=member_users.id
JOIN member_config
ON member_categories.member_id=member_config.member_id
WHERE
member_categories.categories='$category'
GROUP BY member_config.member_id
ORDER BY RAND() limit 0,42";
$rs = mysql_query($new);
while ($row = mysql_fetch_assoc($rs))
{
echo "Result with all member ids";
}
echo "<div class=\"clear\"></div>";
} } <-- (second is a stray from original post)
$memid is not in scope since it appears to be defined inside the loop. Try defining $memid = ''; at the top of your script.. like this.
$memid = '';
$sql2 = "
SELECT *
That way it will be defined when you use it below..

Categories