php looping against ids in mysql - php

Some parts of my page should be shown only for ID's that match this criteria:
<?php
$admins = mysql_query("SELECT id FROM users WHERE is_admin='y'");
?>
so it can be like 1,4 or 1,4,7,3,10,[...] whatever, so these people (ID's) can see smth dedicated, now...
Previously, I had smth like:
<?php
if ($_SESSION['id'] == $customer['id'])
{
echo "<br />";
[...] Dedicated content [...]
}
?>
Which worked until today. Now I need to make it for some more people, database related, if ($_SESSION['id'] == result of the SELECT. How can I achieve that?
Thanks in advice!

just put all those people into the database and check like this:
<?php
$admins = mysql_query("SELECT * FROM users WHERE id='". mysql_real_escape_string($_SESSION['id']) ."' and is_admin='y'");
?>
At that point, if at least one row will return (and yes, it should return exactly one only row) you can proceed with the dedicated content.

You can drop the session id into the query and use mysql_num_rows() to check if you selected any rows:
<?php
$admins = mysql_query("SELECT id FROM users WHERE id=".(int)$_SESSION['id']." AND is_admin='y'");
?>

if the session id is the same as the users id in the database, you could check where session id is an admin in the database. this will simplify your needs

Related

php mysql show result one after another

I wanna build a presence check for our choir in the style of tinder but not as complex.
The database contains names and file paths of pictures of the members. When you click on the "present" or "not present" button, the next picture and name should be shown. In the background, the database table should be updated with true/false for presence. (this will be done later)
My problem is that it almost works, but instead of showing one member, it shows all members with their pictures in one single page.
I understand that I could fire with Javascript to continue and paused php-function but I don't get the clue how.
I tried "break" in the php and call the function again but that didn't work.
<?php
$conn = new mysqli(myServer, myUser, myPass, myDbName);
$sql = "SELECT * FROM mitglieder";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<img class='pic' src='" .$row["folder"]. "/" .$row["img"]. "'><br>" ;
echo "<div id='name'>" .$row["vorname"]. " " .$row["name"]. "</div> <br>";
echo "<img src=''img.png' id='present' onclick='isPresent()'>";
}
} else {
echo "0 results";
}
$conn->close();
?>
<html>
<script>
$( document ).ready(function() {
console.log("Ready");
);
</html>`
You can use php function
mysqli_fetch_all()
assign it on the variable outside the while loop and loop or access the indexes in your code.
For Example:
$data = mysqli_fetch_all();
echo $data[0]['name'];
foreach($data as $item)
{
echo $item['name'];
}
You need a way to establish a "state" between your web page and the PHP backend so that you can step through the data. I suggest something like this:
Use an auto-increment integer primary key for the database. That way you can access the data in index order. Let's name the column id
Have your JS code send a form variable - named something like LAST_ID to the PHP in your get. i.e http://someurl.com/script.php?LAST_ID=0
On your first call to the server, send LAST_ID = 0
In your PHP code, fetch the value like this: $id = $_GET('LAST_ID');
Change your SQL query to use the value to fetch the next member like this:
$sql = sprintf("SELECT * FROM mitglieder where id > %d limit 1", $id); That will get the next member from the DB and return only 1 row (or nothing at the end of data).
Make sure to return the id as part of the form data back to the page and then set LAST_ID to that value on the next call.
You can use a HTTP POST with a form variable to the server call that sets that member id to present (maybe a different script or added to your same PHP script with a test for POST vs GET). I suggest a child table for that indexed on id and date.
I hope that puts you in a good direction. Good luck

if statement depends on existence of data in array, but how to not lose first item in array upon checking it?

It's reminding me of that one question about if the cat exists and it only exists if you open the box. But I digress.
I'm checking the result of a mysql query to find out if a column has data in it, if so I will list the data under a heading. It appears that the act of checking it is "using up" the first in the set of three results in the array that I would otherwise want to display. Here is what I mean:
My query in this case results in the names of three children: Billy Timmy and Sally. I want their names to appear under a heading, "CHILDREN." But sometimes there are no children so I don't want a heading in those cases.
Here is a code sample, missing lots of other content, but I think this is what's pertinent.
<?php
$query2 = SELECT name AS ChildName
FROM family
WHERE TIMESTAMPDIFF(YEAR, bday, CURDATE()) < 24
AND familyid = ".$_GET['id']."";
$result2 = mysql_query($query2);
<?php $row2 = mysql_fetch_array($result2) ?>
<?php IF(!empty($row2['ChildName'])) { ?>
<strong>CHILDREN</strong>
<?php } ?>
<?php while ($row2 = mysql_fetch_array($result2)) { ?>
<ul> <?php echo '<li> ' . $row2['ChildName'] . '</li>';?> </ul>
<?php } ?>
My result in this case is:
CHILDREN
Timmy
Sally
But if I take out the heading check I get all three names under the (permanent) heading.
What am I missing here?
You can first check if any rows are returned using mysql_num_rows($result) method and then do the while loop getting and displaying all the rows. Now you're not getting the first row because you are fetching it to check if it exists.

Knowing which <li> item is selected using PHP and mySQL

I have 2 questions. One is if there is any error in this code.
The 2nd question I want to ask is how do you know which <li> item is selected. Right now, the code performs a search in mySQL for all rows that matches city, language and level and returns the results in a list item.
I want it so that when the user clicks on anyone of the list items, it will goes into another page displaying a more detail description by querying the selected list item.
I have a guess, which is for step 2, I also grab the ID (primary key) for each row and somehow keep that stored within the list but not echo.. Would I need to wrap <a> in <form action="XX.php" method="get">?
<?php
//1. Define variables
$find_language = $_GET['find_language'];
$find_level = $_GET['find_level'];
$find_city = $_GET['find_city'];
//2. Perform database query
$results = mysql_query("
SELECT name, city, language, level, language_learn, learn_level FROM user
WHERE city='{$find_city}' && language='{$find_language}' && level='{$find_level}'", $connection)
or die("Database query failed: ". mysql_error());
//3. Use returned data
while ($row = mysql_fetch_array($results)){
echo "<li>";
echo "<a href=\"#result_detail\" data-transition=\"flow\">";
echo "<h3>".$row["name"]."</h3>";
echo "<p>Lives in ".$row["city"]."</p>";
echo "<p>Knows ".$row["level"]." ".$row["language"]."</p>";
echo "<p>Wants to learn ".$row["learn_level"]." ".$row["language_learn"]."</p>";
echo "</a>";
echo "</li>";}
?>
Your code looks alright from what I can see. Does it not work?
One way would be to get the ID through your SQL-query as well and then add the id to the href in your link. Then you can fetch it through the querystring on the other page, to display the proper post depending on which li-element the user clicked on:
echo "<a href=\"details.php?id=". $row["id"] ."\" data-transition=\"flow\">";
Not sure how you mean "somehow keep that stored within the list but not echo" - it wouldn't be possible to store anything in the list, if it is not echoed, as it then wouldn't be sent to the client. You can of course store the id in a data-attribute on the li-element, which won't be displayed to the user. It will however be visible through the source code! Don't know why that should be a problem though?

How to hyperlink the fetched data?

I am new to php, mysql. currently i'm building a website. with the search query the data is displayed well. (in this case the search query is 'name %like%'). now i want to open another page that will display the profile of any one of the name displayed (when clicked) in the previous query. how to acheive this?
you'll have to select, for example, user_id when searching.
and then, your code could look like
<?php
include("db.php");
$result = mysql_query("SELECT user_id, username FROM user WHERE username LIKE '%".mysql_real_escape_string($_POST['username'])."%'");
echo "<table>";
while($row = mysql_fetch_assoc($result)
{
echo '<tr><td>'.$row['username'].'</td></tr>';
}
echo "</Table>";

PHP catalog, click on item image to display product details

I am trying to build a simple catalog displaying items from a mysql database.
So far I can get the items to display in a list format including the images stored as a path in the items table in a field called "path".
I would like the ability to be able to click on the item image and it takes you to a dedicated page showing you the details of that product. The link would correspond to which ever item you click on based on the data in the item table.
So far I have the following code;
<?php
session_start();
require "connect.php";
$date = date("d-M-Y");
$query = "select * from item order by date && time asc limit 0,3";
$query2 = "select * from users where userid = ".$_SESSION['userid'];
$result = #mysql_query($query, $connection)
or die ("Unable to perform query<br>$query");
?>
<?php
while($row= mysql_fetch_array($result))
{
?>
<?php echo $row['item'] ?>
<?php echo $row['description'] ?>
//Code for Image Link
<a href='<a href='<?php echo $row['path']?>'><img src="<?php echo $row['path']?>
<?php
}
?>
The above code allows you to click on the image and it takes me to http://127.0.0.1/steal/%3Ca%20href=
However I don't know what I would need to enter in order for it to take me to a page that shows the product?
Please Help
Many Thanks
I don't know your database schema, so I'm taking some liberties here about your columns for the product. But, try this:
<img src="<?php echo $row['path']?"/>
I also am not sure what your product page URL is either, so change product_page.php to whatever template you are using to display your products.
You need to create another page called for example showProductsOrder.php that accepts the orderID and then does output the content.
showProductsOrder.php?orderID=1
<?php
SELECT * FROM order WHERE orderID = $_GET['orderID']
while(fetch()) {
//> Show product here
}
?>
Also please don't use mysql_query. Use php.net/pdo
Addendum
Show all products
As yes123 said you would need to create another page, but i if you have a problem creating the link here is my suggested solution:
Replace your link with:
<?php echo ("<a href='".$row['pagePath']."'><img src='".$row['imagePath']."' /></a>";?>
$row['pagePath'] is the page that it will load when the image is clicked on, and
$row['imagePath'] is where the image is stored on your pc / server.

Categories