How to make this code to loop 10 times per page? - php

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbsql", $con);
$result = mysql_query("SELECT * FROM testimonial where approved='Yes'");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Review</th>
<th>Date</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['full_name'] . "</td>";
echo "<td>" . $row['review'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>

If you are looking for that specific block of data to loop 10 times every time the page is loaded simply use a for() loop
for($i=0;$i<10;$i++)
{
// block of data
}
But I assume that that is not what you are asking since it would be impractical (as far as I can see).
To print 10 results, add
limit 10
to the end of your query. If you're using pagination, however, you will need to start the limit somewhere (e.g. limit STARTING_NUMBER, NUM_OF_RESULTS)
Good luck!
Dennis M.

replace
SELECT * FROM testimonial where approved='Yes'
with
$offset = 0; //calculate your offset here as per page;
SELECT * FROM testimonial where approved='Yes' limit $offset, 10

Related

PHP script to make a link out of a MYSQL result?

So I made a test script for PHP output of a basic Mysql database. The problem is that I would like to make the auto increment called orderid to be able to become a link that I can click to open up a page that shows more info about that specific ID of that order.
It really seemed hard to find anything online and I have seen databases that have done this.
Also (not in this script) I have used CURRENT_TIMESTAMP but it doesn't put the current time, but does use the current date. What is the best way to get the current time (AM PM or 24 hour then convert it later) for MYSQL or PHP and input it?
Thank you for your help.
<?php
$con = mysqli_connect('localhost','user','password','database');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
else{
$sql="SELECT * FROM custinfo WHERE orderopen = 1";
$result = mysqli_query($con,$sql);
echo "<table border='1' bordercolor='blue' bgcolor='CCFFFF' cellpadding='0'>
<tr><h2> Open Orders:</h2>
<th>OrderID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Phone</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td><h3><font color='blue'>" . $row['orderid'] . "</font></h3></td>";
echo "<td><h3><font color='blue'>" . $row['firstname1st'] . "</font></h3></td>";
echo "<td><h3><font color='blue'>" . $row['lastname1st'] . "</font></h3></font></td>";
echo "<td><h3><font color='red'>" . $row['phone'] . "</font></h3></td>";
echo "</tr>";
}
echo "</table>";
}
mysqli_close($con);
?>
Well, if you just want to add an a element to this line:
echo "<td><h3><font color='blue'>" . $row['orderid'] . "</font></h3></td>";
Then just add an a element to it:
echo "<td><h3><font color='blue'><a href='somePage.php?id=" . $row['orderid'] . "'>" . $row['orderid'] . "</a></font></h3></td>";
Being a link doesn't make it particularly special, it's still just HTML like any other HTML that you're already outputting.

Read More Button

I have the below table in my site...
and what I want is that when i click on the read more link to redirect me to the page view_announcement.php and in this page to display me the whole data for this specific row.
For example, if we click on the link in the second row I want in the view_announcement.php to load all the data for this specific row.
My code in order to display this table is this...
<?php
$q = ($_GET['q']);
$con = mysqli_connect('localhost','root','smogi','project');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"project");
$sql="SELECT author,category,subject,content FROM announcements WHERE category = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Author</th>
<th>Category</th>
<th>Subject</th>
<th>Content</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['author'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
echo "<td>" . 'Read More' . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
The view_announcement.php file doesn't contain any code yet because i dont know what to write.
One way to do it is to append a query variable to the "Read More" links. You'll probably need a unique identifier, such as an ID number, on your announements table. If you don't have one yet, I suggest adding one and setting it up to auto-increment.
You would want to modify your query to include the unique ID number:
$sql="SELECT id,author,category,subject,content FROM announcements WHERE category = '".$q."'";
Then you would modify the loop which prints your table out to include those unique IDs in the URL to view_announcement.php
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['author'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
echo "<td>" . 'Read More' . "</td>";
echo "</tr>";
}
And in your file view_announcement.php, you would make another SQL query to get the full data for a specific row, like this:
$sql="SELECT * FROM announcements WHERE ID = '".$_GET['id']."'";
If you click any button, that redirects to view_announcement.php file, where you can get the subject values.
Use that subject values in your query to get all the details which relates to that subject.

How to add ajax pagination?

I am using this
PHP Ajax example from w3schools.This is working fine but it's not an efficient solution when the record list will be huge.So i want to add pagination to handle large records.How can i add pagination (Ajax pagination so that i can avoid page reloading) with this?
Here is my code:
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','peter','abc123','my_db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM user WHERE type_no = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
What I normally do is use the LIMIT feature in mysql. When I look at your example I would assume that the sql statement would only return one result.
Lets assume you had a table called user with many users if you had a query like this:
SELECT * FROM user
You would get all the users back in one query. This is not practical for most uses. The most elegant solution in my opinion is the LIMIT feature.
For example if I wanted to see results in a range:
SELECT * FROM user LIMIT 0,50 #return first 50 results - records 0 to 49
SELECT * FROM user LIMIT 50,50 #return the next 50 results - records 50 to 99
LIMIT syntax looks like this LIMIT <offset>,<count> Where offset is the starting result (starts at 0) and count is the number of results you want back.
So lets assume you had pages size of 50 and and were on the 5th page you would want:
LIMIT 100,25
Handling this at the database level is the simplest and quickest method.

Using PHP variable in mysql_query string

OK guys. I have a somewhat complicated issue with passing PHP variables into the mysql_query string.
The $_GET['date']; when passed will contain something like: 2015_01_07_1
I need to have the GET data passed into the table names using the $week variables.
<?php
$week= $_GET['date'];
$con=mysqli_connect("localhost","root","mypassword","beerhandpoker");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query
($con,
"SELECT games_brixx_gastonia_'$week'.rank, players_brixx_gastonia.name, games_brixx_gastonia_'$week'.points
FROM games_brixx_gastonia_'$week', players_brixx_gastonia
WHERE games_brixx_gastonia_'$week'.email = players.email
ORDER BY games_brixx_gastonia_'$week'.rank
LIMIT 20"
);
echo "<table>
<tr>
<th>Rank</th>
<th>Name</th>
<th>Points</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['rank'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['points'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Change the string literal to:
"SELECT games_brixx_gastonia_$week.rank,
players_brixx_gastonia.name,games_brixx_gastonia_$week.points
FROM games_brixx_gastonia_$week, players_brixx_gastonia
WHERE games_brixx_gastonia_$week.email = players_brixx_gastonia.email
ORDER BY games_brixx_gastonia_$week.rank
LIMIT 20"
You have to remove the ' characters;
It's going to the db as games_brixx_gastonia_'2015_01_07_1'.rank
Why do you put single quotes? It should work:
SELECT games_brixx_gastonia_{$week}.rank, players_brixx_gastonia.name, games_brixx_gastonia_{$week}.points
FROM games_brixx_gastonia_{$week}, players_brixx_gastonia
WHERE games_brixx_gastonia_{$week}.email = players.email
ORDER BY games_brixx_gastonia_{$week}.rank
LIMIT 20
Anyway, I'd rather advice you to use statement instead. Check it out:
http://php.net/manual/pt_BR/mysqli.prepare.php
Just remove the ' characters. Otherwise the query would try to get data from the table games_brixx_gastonia_'2015_01_07_1' and not games_brixx_gastonia_2015_01_07_1.

Create an HTML5 range "slider" that does the same as the following drop down menu

I have a drop down menu, which is filled from a database. When I select a value in the menu it displays a table of the data selected from the database. I'd like to change this to an HTML5 range slider. So far with no luck. I also want to show the values (dates) beside the range as I move along it.
This is the code to the drop down menu:
// Set SQL string
$query = "SELECT * FROM Test";
// Execute SQL
$result = mysql_query($query);
// Find number of rows in the resulting recordset array
$num = mysql_numrows($result);
// Initialise loop counter
$i = 0;
echo ("<form><select name='users' onchange='showUser(this.value)'>");
// Loop through recordset until end
while ($i < $num) {
// Associate variables for result at position i at table location specified
$Time = mysql_result($result, $i, "Time");
// Echo each entry as an OPTION for the Select List
echo ("<option value=\"$Time\">$Time</option>");
// Increment Loop Counter
$i++;
}
echo ("</select></form><br>");
gettime.php:
$sql = "SELECT * FROM Test WHERE Time = '" . $q . "'";
$resultb = mysql_query($sql);
if (!$resultb) {
echo "<p>The following SQL failed</p><p>" . $sql . "</p>";
}
echo "<table border='1'>
<tr>
<th>Time</th><th>First PC Room</th>
<th>First Group Study Room 1</th>
<th>First Group Study Room 2</th>
<th>First Main Room</th>
</tr>";
while ($rowb = mysql_fetch_array($resultb)) {
$bmsTime = $rowb['Time'];
//Convert Excel Timestamp of DB to Unix Timestamp
$unixtime=($bmsTime-25569)*86400;
$readable=date('l jS \of F Y h:i:s A',($unixtime));
echo "<tr>";
echo "<td>" . $readable . "</td>";
echo "<td>" . $rowb['firstPCroom'] . "</td>";
echo "<td>" . $rowb['firstGrpStdyRm1'] . "</td>";
echo "<td>" . $rowb['firstGrpStdyRm2'] . "</td>";
echo "<td>" . $rowb['firstmainroom'] . "</td>";
echo "</tr>";
}
echo "</table>";
Below is what I have so far on the "slider":
echo "<input id='slider' type='range' min='0' max=\"$num\" step='any' />
<span id='range'> </span>";
?>
<script>
var selectmenu=document.getElementById("slider");
var colorchange;
selectmenu.onchange=function changecolour(){
if (selectmenu.value<"0.5")
{colorchange=0}
else if (selectmenu.value>="0.5") {colorchange=Math.round(selectmenu.value)}
document.getElementById("range").innerHTML=colorchange;
}
</script>
Any help would be greatly appreciated! Thanks
Here is a jsFiddle that updates a table row using jQuery. Presumably you would replace the data with an AJAX call.
If you were a little more specific about where exactly you were having trouble, someone else may be able to tailor a solution that better fits your needs.

Categories