MySQL Jquery update array? - php

i have a small website where people can submit name ideas for a project i am working on
Two things i want to implement are an upvote button (+1 to current score)
and when someone submits a name it will update the mysql array using ajax.
I am very new to Ajax and i have managed to work some code together that submits a name without leaving the current page.
This is my code for the mysql array that displays underneath the name submission form.
How can i link this upto a form submit button for refresh when a name is submitted?
<?php
$result = mysql_query("SELECT * FROM namevotes ORDER BY upvote desc")
or die(mysql_error());
echo "<center>";
echo "<table border='1' id='tablea'>";
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo $row['name'];
echo "<tr><td>";
echo $row['upvote'];
echo "</td></tr>";
}
echo "</table>";
echo "</center>";
?>
I am want to add a button next to each row that will increase the "upvote" by one, i know how to rig one up that will manually upvote a single entry, im not sure how to direct it to each array entry.

Method is very simple:->
1> add button in while clause using <button id="{row['id']}">like me</button>
2> create a jquery element in webpage that onclick on hitlike button trigger a event: fetch data from url by get {or post} method with data ?hitlike=id
3> before code in php tag, put if statement that if(isset($_GET['id'])) then UPDATE table SET likes=likes+1 WHERE id=$_GET['id']
4> rest code will be the same to show data
//I haven't tried this but I think it should work

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

displaying a single row of data in php using pdo

<?php
if (!empty($_POST['modify']) && !empty($_POST['input']) && !empty($_POST['choose'])) {
$val=$_POST['modify'];
$input=$_POST['input'];
echo"<table border=6><tr><th>id_etu</th>
<th>Nom</th><th>Prénom</th><th>email</th>
<th>mot de passe</th><th>Année</th></tr>";
$sql= "SELECT * FROM etudiant WHERE '$val'='$input'";
$sth=$conn->query($sql);
$res= $sth->fetchAll(PDO::FETCH_ASSOC);
foreach($res as $row){
echo "<tr>";
echo "<td>".$row['id_etu']."</td>";
echo "<td>".$row['nom']."</td>";
echo "<td>".$row['prenom']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['motdepasse']."</td>";
echo "<td>".$row['annee']."</td>";
echo "</tr>";
}
}
?>
Hey everyone, i'm still a beginner in php and using the PDO statement, and right now i'm having trouble displaying a single row of data on a php page. just to put you more in the context.
i have first a form where i choose the field by which i wanna display the row then a input where i type the value of the field then a submit button to send the form, then as a result i should get a table with one row, but instead i get the entire content of the table in the database. Help PLease.

jquery inside a while statement

I am trying to create a drop down menu that shows more information about each person displayed when the person is clicked on.
//Get users information
$sql = "SELECT user.uid, user.first, user.last, user.email, user.rating
FROM user
INNER JOIN team_members ON user.uid=team_members.uid
WHERE tid = '$tid'
ORDER BY user.last ASC";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
//name as button(displayed on page load)
echo "<button class='player' type='submit'>
".$row["first"]." ".$row["last"]."</button><br>";
//hidden information to be shown once button clicked
echo "<div class='player_info' style='display:none'>
".$row["email"]." ".$row["rating"]."
</div>";
}
$(document).ready(function(){
$('button.player').click(function(){
$('div.player_info').toggle();
});
});
The issue I am having is that when clicked it displays all information in each loop. So if there are 5 people and you click one name, each person populates information. The information corresponds to each person but I only want to show the info of the person clicked.
I thought this would mean that I need to somehow uniquely identify each class but cant seem to figure it out and make it work. Or maybe there is a better way to run my loop to solve the problem. Any input is appreciated. Thank you.
You can use HTML data- attributes to correlate each button to a div. Then modify the JavaScript to use the embedded data.
In the below snippet, I added data-uid attributes to both the button and the div that are populated with the uid from the database.
When the button is clicked, the data-uid is read and used to select the div with corresponding data-uid.
Another small improvement that I made was to change the type attribute on the button to button. A value of submit means that the button should be used for submit a form which is not what you are doing here.
//Get users information
$sql = "SELECT user.uid, user.first, user.last, user.email, user.rating
FROM user
INNER JOIN team_members ON user.uid=team_members.uid
WHERE tid = '$tid'
ORDER BY user.last ASC";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
//name as button(displayed on page load)
echo "<button class='player' type='button' data-uid='".$row["uid"]."'>
".$row["first"]." ".$row["last"]."</button><br>";
//hidden information to be shown once button clicked
echo "<div class='player_info' style='display:none' data-uid='".$row["uid"]."'>
".$row["email"]." ".$row["rating"]."
</div>";
}
$(document).ready(function(){
$('button.player').click(function(){
$('div.player_info[data-uid='+$(this).attr('data-uid')+']').toggle();
});
});
Wrap your .player and .player_info in a <div> and do some toggling with JQuery like so:
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<div class='player-container'>
//name as button(displayed on page load)
echo "<button class='player' type='submit'>
".$row["first"]." ".$row["last"]."</button><br>";
//hidden information to be shown once button clicked
echo "<div class='player_info' style='display:none'>
".$row["email"]." ".$row["rating"]."
</div>";
echo "</div>
}
$(document).ready(function(){
// hide all of the player_info containers
$(".player_info").hide();
$(".player-container").click(function() {
// this selects any child element of the clicked container with the
// classname .player_info and toggles it open or closed
$(this).children(".player_info").slideToggle();
});
});
You aren't specifying which player_info you want to toggle, $('div.player_info') will select all of them. A quick and dirty way to solve this would be to use select the next sibling with jQuery, relative to the button, like so:
$(document).ready(function(){
$('button.player').click(function(){
$(this).next().toggle();
});
});

How to make a button delete a row in my database that's also in my HTML?

I have some codes which pretty much gives me all of the Emails and Times from my database and puts it in my HTML. For every Email there is 1 Time. I pretty much want a delete button next to all of the Email+Time and when the button is pressed I want it to delete just that Email+Time. This is my code which gives me all the Emails + Times from my database:
require_once"database.php";
$result = $db->query("SELECT * FROM reserveringen");
if($result->num_rows != 0) {
$message = array();
while($rows = $result->fetch_assoc()) {
$Email = $rows["Email"];
$Tijd = $rows["Tijd"];
$message[] = "$Email $Tijd <input type='button' value='Verwijder afspraak' name='verwijderen'/>";
}
}
<?php
if($message) {
foreach($message as $value) {
?>
<p><?= $value; ?></p>
<?php
}
}
?>
I've tried some stuff and I do have a delete button next to every Email+Time, but how do I get it that when it's pressed to just delete the Email+Time that's next to it.
http://prntscr.com/5x08bl
EDIT:
Okay, let me try to be a bit more specific.
In my database I have a table called "reserveringen". In that table there are 2 columns called "Email" and "Tijd" Everytime when I add an Email and Tijd it automatically places a button next to it in my HTML.(See printscreen above). In this printscreen you can also see that there are 3 Emails + Tijds at the moment and they all have their own button next to it. I'm not a PHP-expert but I want that if I click on 1 of those buttons it deletes the Email + Tijd that's next to it. Not just in my HTML, but also in my database. I hope this is a bit more specific.
Your possible solution is to add an anchor tag on the button and post the id of the respective content along with it.
The anchor href has the value of the page that performs the deletion query of depending upon the received content id.
<input type='button' value='Verwijder afspraak' name='verwijderen'/>
where delete.php executes your deletion query according to the id(email id) you pass along with it.
On deletion page simply get this id with $_GET['id'] and perform the deletion query on the table.
After deleting redirect the page using header('Location:yourpage.php')
Surround your button with <a> tag and in href pass path to your controller and id that u want to delete
while($rows = $result->fetch_assoc()) {
$Email = $rows["Email"];
$Tijd = $rows["Tijd"];
$message[] = "$Email $Tijd <a href='delete?id=$Tijd'><input type='button' value='Verwijder afspraak' name='verwijderen'/></a>";
}
}
and then, in scirpt under delete, get the id that you passed via $_GET['id'] and remove it from database and then redirect to your view.

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?

Categories