Im trying to copy data to clipboard using zclip jquery. problem that im facing is when i copy it will copy everything in the database in field "name". but what i want is to copy a single name that user clicked. if the click another copy that. not the whole thing. can anyone tell me how to do this. thanks.
this is the zclip code
$(document).ready(function(){
$('p#copy').zclip({
path:'js/ZeroClipboard.swf',
copy:function(){return $('div#copy').text();}
});
MySQL loop:
$query = "SELECT * FROM names ORDER BY id desc";
$result= mysql_query($query) or die ('Mysql Error');
while($row = mysql_fetch_array($result)){
echo '<div id="copy">'.$row['name'].'</div>';
echo '<p id="copy">copy</p>';
}
zclip site: http://www.steamdev.com/zclip/
The HTML you generate is not valid which is hindering you in the end to achieve the result you're looking for: in HTML an ID can only be used once (see: Element identifiers: the id and class attributes). You are using the same ID for multiple DIVs. That can not work, as an ID must be unique across the whole HTML document.
Instead assign one ID to each DIV (e.g. by using a counter) and then apply the jquery function on the specific DIV only.
$idCounter = 0;
while($row = mysql_fetch_array($result))
{
$idCounter++;
echo '<div id="copy-div-', $idCounter, '">', $row['name'], '</div>';
echo '<p id="copy-p-', $idCounter, '" class="copy-click">copy</p>';
}
Code-Example: Create multiple IDs with a counter variable
Related
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
I am going to word this to the best of my ability. I have added links to the pages I need help with and their are instructions on there as well. I am new to php and need help please, thank you.
P.S.
I would like to keep the code I have unless you have a better one.
MY QUESTION --- I have created buttons with words from my database table. What I need to know is how to add a link button to the word you click on and this link will open up my articlestagresults.php page and show the word that was selected as well as list the articles according to the word they selected from the tag cloud. Please look at my two web pages and see what I am trying to do.
Here are my two pages with notes in them to better show what I mean.
https://livinghisword.org/articlestagresults.php ----
https://livinghisword.org/articlestagcloud.php
enter code here
THIS IS THE CODE FOR articlestagcloud.php --- below
$sql = mysqli_query($db_conx, "SELECT word FROM articles GROUP BY word ORDER BY word ASC");
while ($row = mysqli_fetch_array($sql)) {
$word = $row['word'];
ksort($word);
echo "<div class='cloudbox tag'>$word</div>";
}
Thank you very much for all the help coming!
You just need to wrap the word in anchor tag like this
echo "<div class='cloudbox tag'><a href = 'articlestagresults.php?word=" . $word . "'> " . $word . "</a></div>";
In articlestagresults.php, You can simply get the word by print_r($_GET['word']); This will give you the selected word from the previous page.
articlestagresults.php
$word = $_GET['word'];
$sql = mysqli_query($db_conx, "SELECT * FROM articles WHERE word = '$word'");
while ($row = mysqli_fetch_array($sql)) {
print_r($row);// It will contain the row data
//SHOW the HTML
}
I would suggest a simple javascript function for that, such as:
<script type='text/javascript'>
function bindEvents(){
var col=document.querySelectorAll('div.cloudbox');
if( col ){
for( var n in col ){
if( col[ n ].nodeType==1 ){
col[n].addEventListener('click',function(e){
location.href='/articlestagresults.php?word='+this.innerHTML;
}.bind( col[ n ] ), false );
}
}
}
}
document.addEventListener('DOMContentLoaded', bindEvents, false );
</script>
That should append the word=value string to the querystring - so your sql would use the value of $_GET['word'] to process the request.
There are several ways you could do this - rather than using GET you could submit a form using POST which would keep the url in the browser as https://livinghisword.org/articlestagresults.php
I've written a PHP script that can populate a table in a particular way so that multiple events (or no events) can be put in one square in an HTML - similar to the layout a calendar would have. But, there's a problem, the while statement I created to fill in squares in the table when there is no data doesn't detect when there is data, and fills the entire table with empty squares. This is what the output looks like (The page is styled using Bootstrap 3). From the mysql data I have provided, these events should be in the square at {Period 1, Monday}.
Here is my data in a mysql database; mysql data
Here is a snippet of the part of the page related to this table;
<?php
$query = "SELECT * FROM configtimetabletwo WHERE term = ".$term." AND week = ".$week." ORDER BY period, day LIMIT 100;";
$results = mysqli_query($conn, $query);
$pp=1; //The current y value of the table
$pd=0; //The current x value of the table
echo '<tr><td>';
while($row = mysqli_fetch_row($results)) {
while((pd!=$row[3] or $pp!=$row[4]) and $pp<6){ //This while statement fills in empty squares and numbers each row.
if($pd==0) {
echo $pp."</td><td>";
$pd++;
}
elseif($pd<5){
echo "</td><td>";
$pd++;
}
else {
echo "</td></tr><tr><td>";
$pd=0;
$pp++;
}
}
echo '<a href="?edit='.$row[0].'" class="label label-default">';
echo $row[5].' '.$row[6].' - '.$row[7]."</a><br>";
}
echo "</td></tr></table>"
?>
I haven't been able to figure out why this happens so far, thanks in advance to anyone who has any idea what's going on.
In the comments below my question, pavlovich pointed out the error. In this case, it was simply an issue of forgetting to use a $ to reference a variable. It would seem that this doesn't throw an error in a while statement like it would elsewhere.
I have a problem on selecting an id on div. I use a while loop on my div.
So i'am creating a unique ID using the id row on my sql. My question is, is it possible to select the id using Javascript? I use bootstrap modal to update my information details (i dont actually know how to ask this question because my english is poor).
example:
while($row = mysqli_fetch_array($result)) {
<div id="'details-'<?php echo $row['Room_ID'];?>">
//code
</div>
}
To fix your Php code:
while($row = mysqli_fetch_array($result)) {
echo "<div id='details-" . $row['Room_ID'] ."'>";
// CODE
echo "</div>";
}
The problem you got in the id, where you stopped the value by ' already, so the result was something like:
<div id='details-'28>//Code</div> and fixed to <div id='details-28'>//Code</div>
and to select the ID with pure JavaScript use document.getElementById( ID )
If you are not sure of the ID and you just wanna select all items, add a class to the item and select them all by document.getElementsByClassName
So to select all your stuff by javascript:
1) Add into your PHP code class called "details"
2) Create a javascript to select all items with the class name "details"
3) Do a foreach loop for the selected items and split them by "-" to devide the "details" from the actual ID
4) Do whatever you want now with the ID
Practical showcase:
Php:
while($row = mysqli_fetch_array($result)) {
echo "<div class='details' id='details-" . $row['Room_ID'] ."'>";
// CODE
echo "</div>";
}
JavaScript:
<script>
var itemDetails = document.getElementsByClassName("details");
for(var i = 0; i < itemDetails.length; i ++) {
var ID = itemDetails[i].getAttribute("id").split("-")[1];
// Now you got the ID stored in the variable ID
</script>
Here you got an example on JSFiddle
As for as my understanding from your code, you are want to create a div with id details from your database. For that you should correct your code as given blew
<?php
while($row = mysqli_fetch_array($result)) {
echo "<div id='details-'". $row['Room_ID'];">";
//code
echo "</div>";
}
?>
If it is problem to select it and perform some manipulations using jQuery/javascript you may consult the official API documentation at
This link
needing some advice.
I am wanting to include 4 drop down lists on a website, which all contain data from different fields in a mysql table.
I then want to be able to press a submit button and display the required data on a webpage.
I am having trouble with knowing what programming language to use and also finding it difficult to find any tutorials. Any help would be greatly appreciated.
Thanks
You mean a HTML dropdown list or just a select dropdown in a form?
If you mean a select dropdown in a form you could do something like this:
<?PHP
$query = mysql_query("SELECT value FROM table ORDER BY value ASC") or die(mysql_error());
$result = mysql_num_rows($result);
// If no results have been found or when table is empty?
if ($result == 0) {
echo 'No results have been found.';
} else {
// Display form
echo '<form name="form" method="post" action="your_result.php">';
echo '<select name="list" id="lists">';
// Fetch results from database and list in the select box
while ($fetch = mysql_fetch_assoc($query)) {
echo '<option id="'.$fetch['value'].'">'.$fetch['value'].'</option>';
}
echo '</select>';
echo '</form>';
}
?>
And then in your_result.php you should fetch the data from your MySQL database based on value from the (when you fetch use mysql_real_escape_string):
<?PHP $_POST['list']; ?>
You could also do everything in just one file, but thats up to you. Try to use google, there are dozens of tutorials out there.