I am having some difficulty using PHP with jQTouch. I am fairly
confident with JavaScript however my PHP skills are little to none.
I am creating an application for my final year project at University
what displays football rumours posted by different users. My problem
is as follows:
I have one screen that displays each individual rumour, using a while
loop in PHP I am able to get each rumour from the database and display
them correctly. However I want to be able to click on one rumour which
then displays this rumour in a different screen, along with options to
reply/share etc. However I do not know how to tell which rumour has
been clicked on.
Snippets of my code:
All rumours page:
<?php
$q1 = "SELECT * FROM tblrumours;";
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_assoc($r1) ){
?>
<a class="rumourTag submit" id="<?php echo $row1['rumourID']; ?>">
<div class='oneRumour'>
<div class='standardBubble'>
<p>
<?php
$userID = $row1['userID'];
$q2 = "SELECT * FROM tblusers WHERE userID = $userID;";
$r2 = mysql_query($q2);
while( $row2 = mysql_fetch_array($r2) ){
$username = $row2['username'];
$teamID = $row2['teamID'];
}
$q5 = "SELECT * FROM tblteams WHERE teamID = $teamID;";
$r5 = mysql_query($q5);
while( $row5 = mysql_fetch_array($r5) ){
echo "<img src='img/".$row5['teamPicture']."' alt=''
class='teamImg' />";
}
?>
<span class='username'>
<?php
echo $username;
?>
</span>
<br/>
<span class='rumourMsg'><?php echo $row1['rumourText']; ?></
span>
</p>
</div>
</a>
SINGLE RUMOURS PAGE:
<?php
$q1 = "SELECT * FROM tblrumours WHERE rumourID = 1;"; /* NEED
TO SELECT WHERE RUMOUR ID IS THE ONE THAT IS CLICKED */
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_array($r1) ){
?>..........
I have tried using Session variables, storing the ID's in an array,
creating a separate php file for the single rumour page, and all to no
avail. I am guessing I have to use AJAX in some way, but I have no
idea where to even begin. Any help is greatly appreciated!
Thanks!
If you need to click on a rumour to see more details about it, you could always output in the HTML a unique value used to reference that rumour in the DB.
e.g. have <span class='rumourMsg' id='rumourName'> where rumourName is a unique value stored in your database to reference that rumour. Then when a user clicks to see more details, you can make a request to the PHP page with that value and return the content.
e.g. rumourDetails?rumourName=uniqueRumourName
(make sure to escape all your data properly to avoid SQL injection vulnerabilities.)
Related
so I have a list of people with their city name and contact numbers in mysql database which is displayed on my website. I want to know which person was contacted by a visitor.
Here is a snippet of my code:
<?php
$city = $_POST['city'];
$sql = "SELECT * FROM users WHERE city = '$city'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
?>
<tr>
<td class="pl-4"><?php echo "<h3>" .$row['fname']. " " .$row['lname']. "</h3>"; ?>
<BUTTON onClick="contactclick();" class="track btn btn-outline-info p-2"><span class="icon-phone"></span> Contact</BUTTON>
</td>
<script>
function contactclick() {
<?php
$sql7 = "SELECT * FROM users WHERE id = '$id' LIMIT 1";
$result7 = mysqli_query($conn, $sql7);
$row7 = mysqli_fetch_assoc($result7);
$firstname = $row7['fname'];
$lastname = $row7['lname'];
$city7 = $row7['city'];
$txt = 'Call received by:'.$firstname.' '.$lastname.' of '.$city7.'';
$file = fopen('drivers-contacted.txt','a');
fwrite($file,$txt);
?>
}
</script>
</tr>
As you can see from the code I have tried making a text file and adding info into that file everytime 'contact' button is clicked. But it adds the name of all people in the list instead of 1 who was actually contacted. How can I solve this? Also, is there a better way to get the information I want, like which user was contacted from my database list?
PS : I'm new to coding
In general: you are trying to communicate in the direction of client-side to server-side when you want to do a server-side action on a button click. Embedding PHP code inside the client script is not the correct way to do this. Please look up AJAX. A library called jQuery is fairly easy to use and has a simple interface for AJAX.
In addition: unless there is some code missing from this snippet, what this appears to do is create n contactclick() functions, each with its own ID. You need to take the function out of the loop, and have it accept id as a parameter. Then you can do an AJAX call from this function and your back-end (PHP) code will write to a file.
I thought I would simplify my question and start with trying to solve one problem at a time instead of two. The -1 rating tells me I was not clear enough, so I will try again. My original post is quoted below.
With the following code I can display data from my table that meets the condition that the category is Scenics:
<?php
$category = $_GET['category'] ?? 'Scenics';
$sql = "SELECT * FROM photo_library ";
$sql .= "WHERE category='" . $category . "' ORDER BY id ASC";
$image_set = mysqli_query($db, $sql);
$image_set_desc = mysqli_query($db, $sql);
?>
What I want to do is have dynamically set. Instead of $category = $_GET['category'] ?? 'Scenics';, is there a way not set the way it is above? I would like it be set by whatever it finds in the 'category' column of my SQL table so that later I can say while category is Scenics and sub_category is Coastal, display just that info (so I will need to do something similar for sub_category).
EDIT: The term I was looking for was array. I want $sub_category to be an array based on what is found in the sub_category column of my SQL table.
I am also aware of the risk of using $_GET, but this would be on a protected site where you only get access if you are a member, and you are only a member if you apply and are manually approved for access. And there is no user input in the protected part of the site. It is strictly a resource site for downloading graphics and images.
I am playing around with having an image gallery be generated using
PHP and My SQL instead of coding everything in HTML on the page. I've
created a test table that has multiple columns, and a working page
where all the info gets pulled in and displayed.
The two main values that will be used to separate image entires from
each other are 'category' and 'sub_category'. The page that is working
only uses the category and works great.
<?php
$category = $_GET['category'] ?? 'Agroforestry';
$sql = "SELECT * FROM photo_library ";
$sql .= "WHERE category='" . $category . "' ORDER BY id ASC";
$image_set = mysqli_query($db, $sql);
$image_set_desc = mysqli_query($db, $sql);
?>
$image_set pulls in the info for the actual image, and $image_set_desc
does the work for the links and file size. This is the pulling in of
the thumbnail:
<?php while($subject = mysqli_fetch_assoc($image_set)) { ?>
<li><a href="<?php echo h($subject['jpg_location']); ?>" title="<?php echo h($subject['title']); ?>" data-title="<?php echo
h($subject['id']); ?>">" src="#" alt="" aria-describedby="">
<?php while($subject_desc = mysqli_fetch_assoc($image_set_desc)) { ?>
<p id="<?php echo h($subject_desc['id']); ?>"><b>Filename:</b> <?php echo h($subject_desc['filename']); ?><br>
<a class="btn btn-default btn-xs mrgn-tp-sm" role="button" href="<?php echo h($subject_desc['tif_location']); ?>">TIFF <span
class="wb-inv">image download () ">JPG image
download ()
The problem is on the next page I am testing this on, there are a lot
more pictures and they are broken up into multiple sub-categories. I
only want to display pictures with certain sub-categories under their
respective headings.
I am very new to PHP, and do not know how to set something like
$sub_category to whatever it finds in that column. I cannot use
$sub_category = $_GET['sub_category'] ?? 'Coastal';
because the sub-category needs to be pulled from the SQL table. I
don't know what I am doing so while
$sub_category = isset($_GET['sub_category']);
doesn't break anything, it also doesn't seem to work. If that somehow
works, then
<?php while(($subject = mysqli_fetch_assoc($image_set)) && ($sub_category == 'Coastal')){ ?>
is not because nothing is generated on the page. I'm not sure if my
problem is with defining $sub_category, the while loop, or both.
First off even if your site is restricted to a subset of users your still leaving room for many attacks which could lead to attackers being able to mess with your database structure and even worse obtain sensitive data.
Regarding your problem please have a look at normalization. Currently every picture has a set category written in the category field which leads to your current confusion/problem because you now want to gather categories from every picture within your database.
If you were to normalize the category field to a separate table and link them via a foreign key you could then firstly query all categories and then group the images by the categories after looping trough.
Here's a bit of non optimized pseudo code that should give you an idea as to what I mean:
$categorizedImages = [];
$catSql = "SELECT * FROM categories";
$categories = mysqli_query($db, $sql);
foreach($categories as $category) {
$imageSql = "SELECT * FROM photo_library WHERE category='" . $category['id'] . "' ORDER BY id ASC";
// I cant recall if the access from mysqli_query is via array or ->
$categorizedImages[$category['id']] = mysqli_query($db, $imageSql);
}
var_dump($categorizedImages); exit;
This is the code which selects from DB and sets the image tag.
<div>
<?php $query = mysql_query("SELECT * FROM company where sn='1'");
while($rows = mysql_fetch_assoc($query)){
$logo = $rows['logo'];
$password = $rows['password'];
$phone = $rows['phone'];
}
?>
<img src="<?php echo $logo ?>"/>
</div>
When we get this and set on textarea then we want this query{which save in db} executed. and output show only Logo name.
But this time this show full query which save in db.
we want get this output on textarea:
<div><img src="logoname"/></div>
You are using mysql extension, which is deprecated. You should use mysqli instead.
The loop overwrites your variables ($logo, $password, $phone) in every iteration, so it makes no sense until you're fetching single row.
But if you're fetching single row, then you don't need a loop:
<?php
if ($r = mysqli_query($connection, "SELECT * FROM company WHERE sn = 1")) {
$company = mysqli_num_rows($r) ? mysqli_fetch_row($result)[0] : null;
mysqli_free_result($r);
}
?>
<img src="<?php echo empty($company) ? 'nophoto.png' : $company['logo']; ?>" />
Replace
SELECT * FROM company where sn='1'
With
SELECT * FROM company WHERE sn=1
If you take out the apostrophes, that might solve your problem since the value stored in your database is most likely not a string. Also you should have WHERE in capital letters.
Let me know if that answered your question! :)
I'm trying out my hand at php at the moment - I'm very new to it!
I was wondering how you would go about selecting all items from a mySQL table (Using a SELECT * FROM .... query) to put all data into an array but then not displaying the data in a table form. Instead, using the extracted data in different areas of a web page.
For example:
I would like the name, DOB and favorite fruit to appear in one area where there is already say 'SAINSBURYS' section hardcoded into the page. Then further down the next row that is applicable to 'ASDA' to appear below that.
I searched both here and google and cant seem to find an answer to my strange questions! Would this involve running the query multiple times filtering out the sainsburies data and the asda data where ever I wanted to place the relevant
echo $row['name']." ";
echo $row['DOB']." "; etc etc
next to where it should go?
I have got php to include data into an array (I think?!)
$query = "SELECT * FROM people";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
echo $row['name']." ";
echo $row['DOB']." ";
echo $row['Fruit']." ";
}
?>
Just place this (or whatever your trying to display):
echo $row['name']." ";
Anywhere you want the info to appear. You can place it within HTML if you want, just open new php tags.
<h1>This is a the name <?php echo $row['name']." ";?></h1>
If you want to access your data later outside the while-loop, you have to store it elsewhere.
You could for example create a class + array and store the data in there.
class User {
public $name, $DOB, $Fruit;
}
$users = new array();
$query = "SELECT * FROM people";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$user = new User;
$user->name = $row["name"];
$user->DOB = $row["DOB"];
$user->Fruit = $row["Fruit"];
$users[$row["name"]] = $user;
}
Now you can access the user-data this way:
$users["USERNAME"]->name
$users["USERNAME"]->DOB
$users["USERNAME"]->Fruit
I've scoured the web for a tutorial about this simple task, but to no avail. And so I turn to you helpful comrades. Here's what I need to do:
I have a MySQL database with an Events table. I need to create a PHP web page with a list of the Event titles, and each title must be a link to the full details of the Event. But I want to avoid having to create a static page for each event, primarily because I don't want the data entry volunteer to have to create these new pages. (Yes, I realize that static pages are more SEO friendly, but I need to forego that in this case for the sake of efficiency.)
I've seen PHP url syntax with something like this:
pagename.php?id=20
but I don't know how to make it work.
Any and all help greatly appreciated.
Thanks!
Kip
This is basic php. You would simply query the DB for the event details before the page headers are written and write the html accordingly.
The first thing I would ask you is if you know how to connect to your database. From there, you query based on the $_GET['id'] value and use the results to populate your html.
Not to be rude, but the question itself suggests you're new to PHP, right? So in order to provide a solution that works we might want to know just how far you got.
Also, you can rewrite your dynamic urls to appear like static ones using apache's mod_rewrite. It's probably a novice level thing if you're interested in "pretty" url's.
MODIFIED ANSWER:
In your loop you would use the id from the query result (assuming your primary key is id)...
while($field = mysql_fetch_array($result)) {
echo "<p class='date'>";
echo $field['month']." ".$field['day'].", ".$field['year'];
echo "</p>";
echo "<h3>";
echo ''.$field['event_name'].'';
echo "</h3>";
}
Then on somepage.php you would use the get var id to pull the relevant info...
$result = mysql_query("SELECT * FROM `calendar` WHERE `id` = '".mysql_real_escape_string($_GET['id'])."');
don't forget to look into mysql_real_escape_string() for cleaning entries.
It's wise to take extra care when you are using $_GETvariables, because them can be easily altered by a malicious user.
Following with the example, you could do:
$foo = (int)$_GET['id'];
So we are forcing here the cast of the variable to a integer so we are sure about the nature of the data, this is commonly used to avoid SQL injections.
lets say you have the php file test.php
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("db", $conn);
$id = $_GET['id'];
$sql = "select * from table where id = $id";
$result = mysql_query($sql, $conn);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[0];
$content = $row[1];
}
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<h1><?php echo $title ?></h1>
<p><?php echo $content ?></p>
</body>
</html>
a dynamic page would be something like that..
Here is the pertinent code for extracting a list of events in November from a table named calendar, with each event having a link to a page called event.php and with the event's id field appended to the end of the url:
$result = mysql_query("SELECT * FROM calendar WHERE sort_month='11'");
while($row = mysql_fetch_array($result))
{echo
"<a href='event.php?id=".$row['id']."'>".$row['event_name']."</a>"
;}
And here is the pertinent code on the event.php page. Note the row numbers in brackets depends on the placement of such in your table, remembering that the first row (field) would have the number 0 inside the brackets:
$id = $_GET['id'];
$sql = "select * from calendar where id = $id";
$result = mysql_query($sql, $con);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[12];
$content = $row[7];
}
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<h1><?php echo $title ?></h1>
<p><?php echo $content ?></p>
</body>
</html>
This works for me, thanks to the help from those above.
$foo=$_GET['id'];
in your example $foo would = 20