I'm trying to create a "load more" button to the news section of my website, but whenever I try anything my mind goes blank and I don't know how to begin making it.
I'm not good at any javascript/jquery/ajax just "some" php.
So far I only have my php script display all the news from my database...
$sql = mysqli_query($con,"SELECT * FROM news ORDER BY date DESC")
or die (mysql_error());
while($row = mysqli_fetch_array($sql)){
$usql = mysqli_query($con,"SELECT * FROM members WHERE id = 1");
while($user = mysqli_fetch_array($usql)){
echo('<li class=""><br/>('.$row['date'].')');
echo ('<a href="/news/'.$row['id'].'" title="'.$user['username'].'" rel="nofollow">');
echo ('<img src="'.$row['img'].'" alt="'.$user['username'].'" class="list_intros_img">');
echo ('<h4>'.$row['title'].'</h4>');
echo ('<p>'.substr($row['content'],0,400).'</p>');
echo ('</li><hr class="line">');
}
}
I suggest you to load a default number of news when you first load the page, map the "load more" button with an ajax request to fetch more news and update your list.
You could use the LIMIT and OFFSET keyword in MySQL to fetch more news. (https://dev.mysql.com/doc/refman/5.0/en/select.html).
In your case, that could be something like : "SELECT * FROM news ORDER BY date DESC LIMIT 10 OFFSET 10" to display news 11 to 20.
I hope this gives you a lead to get started.
Related
I have a website where people upload random things, and the home page displays everything that has been posted in a while loop. I want to make it so it only shows that part of the page that the user has scrolled to instead of displaying all of the rows in that table. It takes about 20 seconds for the whole page to load and I want to narrow that down.
I have tried to use pagination, but I can't find any easy way to do it.
<?php
$db = mysqli_connect("localhost", "root", "", "photos");
$sql = "SELECT * FROM images
ORDER BY id DESC";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result)) {
echo "<td><a href='view_news.php?id=".$row['id']."'><br><center><div id='img_div' class='boxx'>";
echo "<img class='image' style='width:220px;height:124px;float:left;' src='images/".$row['image']."' >";
echo "<h3>".htmlentities($row['title'])."</h3>";
echo "<h5><b>By: </b>".htmlentities($row['user'])."</h5>";
echo "<h6>".htmlentities($row['date'])."</h6>";
echo "<br></div></center></a></td>
";
}
?>
It displays all of them at once, but I want them to be displayed only when the user has scrolled to them. Thanks!
You can do that with an ajax call. Once the user scrolls to the buttom of the page make an ajax call that will return you more rows and append them to the dom.
For ajax requests the easiet would be with jQuery
https://api.jquery.com/jquery.get/
I know that title a bit confusing, so I'll try to explain better here.
I'm trying to create a form where a user inputs a ticket and that ticket gets assigned to a technician based off the service they provide. I have 3 text fields, username,email, Description of the problem. The next field is a select field. I populate this select field by running a php script that will query a database of services offered and return the results.
so something like
<select name="service' id= "Service" onClick="showTechnican()">
<?php include "services_offered.php"?>
</select>
When a user clicks on one of the services, I want the form to query my services_offered table and return the username(s) that provide that service in a div.
<div id="techs"></div>
I'm using a jquery script to do this
<script>
function showTechnician(){
var selectedValue = $("#Service").val();
if(selectedValue.length < 1){
document.getElementById("techs").inner.HTML = "Please Select A Service Above";
return;
}else{
var url = "technicians.php?q="+selectedValue;
$.get(url,function(data,status){
document.getElementById("techs").innerHTML=data;
});
}
}
I've also got the script technicians.php that is querying the database for me
<select name= "tech">
<?php
$q = $GET['q'];
$conn = new mysqli("localhost", "proxy_user",
"my*password","helpdesk");
if(mysqli_connect_errno()){
echo'Unable to connect to database:'.
mysqli_connect_error($conn);
}
else{
$query = "SELECT * FROM services WHERE Service_Descripton LIKE'%".$q.
"%';";
$result = mysqli_query($conn,$query);
if(!$result){
die("Invalid Query:" . mysqli_error($conn));
}
else{
while($row = mysqli_fetch_array($result)){
echo"<option value= "."{$row['User_Name']}>"."{$row['Service_Description']}".
'</option><br/>';
}
mysqli_close($conn);
}
}
?>
my issue is that when I click on a service offered, my div only shows a small empty box. Not really sure why it's not returning anything. I've got plenty of test data in the table. Made sure permissions were correct as well. I've checked the php_error_log and the query isn't failing. I'm not sure how to resolve this. Every other field on the form will insert just fine into the database.
I've attached a photo of the issue. See under "available testers" it just displays a little box.
photo of the problem
I figured it out. I can't spell, had a spelling mistake in the output of my query. Fixed it and everything worked as it should.
I am trying to get this to allow the user to click on a link, which will then change the value of what is submitted in the SQL SELECT Statement, if that makes sense.
My Code so far:
<div class="Tabs" >
<?php
require 'database/connect.php';
$sql = "SELECT DISTINCT Week FROM PMWUpdates";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo '<a class="weeks"> Week' . $row{'Week'} . '</a>';
}
?>
</div>
<div class="Pages">
<?php
require 'database/connect.php';
$sql = "SELECT * FROM PMWUpdates WHERE Week='1'";
?>
</div>
I want the value of SELECT * FROM PMWUpdates WHERE Week="'1'"; to be what link the user has clicked on above, not just always 1. So if the user clicks on the link 2, the SQL changes to SELECT * FROM PMWUpdates WHERE Week="'2'";
I am basically trying to accomplish this, but instead, I want to display the number of tabs, based on the number of weeks in a database, and then, each week will display data from the database about that particular week.
Any help is appreciated.
Thank You
Your statement should be like this...
$sql = "SELECT DISTINCT Week FROM PMWUpdates";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo '<a class="weeks" href="$row['Week_link']"> Week' . $row['Week'] . '</a>';
}
If you mean dynamically changing data on a page which requires PHP to be processed, you have to take a look at AJAX. You can't do it this way, once your script is loaded, there is no more php, only HTML.
If you don't want to use AJAX, you will have to do it in 2 pages. First display links, then display your data related to whatever was selected.
The MySQL extension you are using is deprecated (>= 5.5). Use MySQLi or PDO instead.
What you are trying to accomplish is that where your "2" is listed, should change with the use of a variable. As you want to do it via Javascript, you will need a click event on those links which then submits the variable via AJAX.
I'm a fairly new programmer and I'm currently working on creating a program for a web-based art exhibit that would will display a random image and text using data from a PHP form which requires inputs for both.
I will need to create a PHP form that gathers text and a picture from the visitor.
Example PHP page:
*What is your name?* = Max
*Upload photo.* = pic.jpg
The finished project would be a webpage that - on refresh - would display the image and the text gathered from the PHP form randomly. So on refresh the page would display a random text input (I.E Max, Alan, Mark, etc.) and a random picture (I.E pic.jpg, pic1.jpg, pic2.jpg) I'm guessing from a MySQL database.
The page, on refresh, would therefore display something like this:
Max / pic.jpg
REFRESH
Alan / pic3.jpg
REFRESH
Mark / pic2.jpg
And so on...
This is what I have so far:
<?php
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$pic=($_FILES['photo']['name']);
// Connects to your Database
mysql_connect("mysite.com", "username", "password") or die(mysql_error()) ;
mysql_select_db("db_name") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$name', '$pic')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
Any help on this would be much appreciated!
Thank you!
Pull out the data using
SELECT * FROM table ORDER BY RAND() LIMIT 0,1;
Then pull out the
<?
$sql=mysql_query("SELECT * FROM table ORDER BY RAND() LIMIT 0,1;");
while($row=mysql_fetch_array($sql)){
$name=$row['name'];
$imagename=$row['imagename'];
//write the logic here use echo or close and open php using <? and ?>
//giving an example
echo "Name: ".$name."<br>";
echo "<img src='directory/'".$imagename." width='100' height='100'>";
//OR other way
?>
Name : <? echo $name;?><br>
<img src="directory/<? echo $imagename;?>" width="100" height="100">
<?
}
?>
First you will need to create the html form. I assume you have some basic knowledge about this, so let's assume you do it and post it to post.php. Here you'll need to:
Handle the image.
Insert the name and image reference into the database.
Redirect the user to the landing page (important to avoid refreshing issues like "want to send your data again?").
Fetch a random entry for the database and display it to the user on the landing page.
A few tutorials:
Upload Images Using PHP.
Insert data into database
Pick up a random record from the database Disclaimer: my own question over 1 year ago
However, it's not a 'trivial' task. It'd take few days to someone familiar with the languages, with someoone who is not maybe 1 week provided you start from the ground up.
i have totally no idea about how to do this, so i'm gonna just ask away.
I have a dropdown menu which list dates say
1/2/2013
2/2/2013
3/2/2013
4/2/2013
5/2/2013
6/2/2013
7/2/2013
if you were to select one of the dates, a div will pop out with say 5 choices
A
B
C
D
E
each choices are stored in the database, and if say B item is not available on 2/2/2013, i would have a script to disable it being selected. I've figured how to create that in php, but my ultimate question is
how do you select any of the dates but yet still able to retrieve the 5 choices from a database?
I'm currently doing something like this
function TheDisabler($aa)
{
global $con, $vdate;
$myresult = mysqli_query($con,"SELECT * FROM burger WHERE timeslot = '$aa' AND date = '$vdate'");
list($mycount) = mysqli_fetch_row($myresult);
if($mycount >= 1) {
echo "disabled";
}
}
but i figured that this works only once and if i were to change the date, the items within the div will not change =/
You should have a script that works every time the div element is clicked. So add an onClick event to div and then retrieve the required data from database on that event's handler.
if your table does not have large amount of data, create a php file that behave as if it's a javascript file like that:
<?php
$js = 'data = new Array()';
$sql = 'select ...';
$count = 0;
while($row = fetch_rows($sql)){
// do your check here
$js .= 'data[' . $row['date'] . '] = new Array()';
$js .= 'data[][' . $row['date'] . '][$count] . ' = "'.$row['item'] . '";';
$count++;
}
header('Content-Type: text/javascript');
echo $js;
?>
now you have a javascript array which have every date items, then on each click ask this array for that date key should return you the your options list
I think what you are looking for is the onchange event of the ckeck box. Each time there is a change in the drop down, call the function to retrieve the values from the database.