I am using this pagination class and was looking for a way to make the sql query more dynamic instead of having it hardcoded.
I have a 3 <li> elements that I want to be filter buttons, meaning when a user clicks on one of these elements I want It to send the id so I can use it in a sql query.
So for the $sql = "select * from explore where category='marketing'"; (as seen below). When the user clicks on the 'automotive' button it will change the category above to automotive.
Any help on this would be highly appreciated, Thanks.
This is what my main page looks like:
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery_page.js"></script>
<?php
//Include the PS_Pagination class
include('ps_pagination.php');
//Connect to mysql db
$conn = mysql_connect('localhost', 'root', 'root');
mysql_select_db('ajax_demo',$conn);
$sql = "select * from explore where category='marketing'";
//Create a PS_Pagination object
$pager = new PS_Pagination($conn, $sql, 3, 11, 'param1=valu1¶m2=value2');
//The paginate() function returns a mysql
//result set for the current page
$rs = $pager->paginate();
//Loop through the result set
while($row = mysql_fetch_assoc($rs)) {
echo "<table width='800px'>";
echo "<tr>";
echo"<td>";
echo $row['id'];
echo"</td>";
echo"<td>";
echo $row['site_description'];
echo"</td>";
echo"<td>";
echo $row['site_price'];
echo"</td>";
echo "</tr>";
echo "</table>";
}
echo "<ul id='pagination'>";
echo "<li>";
//Display the navigation
echo $pager->renderFullNav();
echo "</li>";
echo "</ul>";
echo "<ul id='filter'>";
echo "<li id='marketing'>";
echo "Marketing";
echo "</li>";
echo "<li id='automotive'>";
echo "Automotive";
echo "</li>";
echo "<li id='sports'>";
echo "Sports";
echo "</li>";
echo "</ul>";
?>
looks like this line
$pager = new PS_Pagination($conn, $sql, 8, 3, 'param1=valu1¶m2=value2');
should be edited to carry variables to be tested before the sql query is run.... something maybe like:
$pager = new PS_Pagination($conn, $sql, 8, 3, 'param1=automotive¶m2=sports');
then try this
if($_POST["param1"]=="automotive")
{
$sql = "select * from explore where category='automotive'";
}
and work around with it to see if you can get your desired results. I've never used the class before, but it looks like you'll have to play with these params to get your desired result
Related
I am using a class that fetches information from a database and shows that information in a table, along with an edit and delete button. When I click the edit or delete button, I want to add $_GET['id'] to the link, but I am confused on how to accomplish this.
public function select($tName,$from,$where=1){
$key=[];
$con=$this->con;
$sql="SELECT $tName FROM $from WHERE $where";
$data = $con->query($sql);
$fullRec=[];
foreach($data as $k=>$rows){
$fullRec[]=$rows;
}
foreach($rows as $k=>$v){
$key[] = $k;
}
//create table
echo "<table border='1'>";
//create one row with col name selected!!
echo "<tr>";
for($z=0;$z<count($key);$z+=2){
echo "<td>".$key[$z]."</td>";
}
echo "<td>actions</td>";
echo "</tr>";
//create one row for each record comes!!
for($i=0;$i<count($fullRec);$i++){
echo "<tr>";
//create one table data for each record comes!!
for($j=0;$j<count($fullRec[$i])/2;$j++){
echo "<td>".$fullRec[$i][$j]."</td>";
}
echo "<td>EditDelete</td>";
echo "</tr>";
}
echo "</table>";
}
use like this to pass the id of the record in get parameter
echo "<a href='your_route?del=".$rows['id']."'>DELETE</a>";
Hey all I am stuck on displaying a table on my webapge using php. Everytime I try something new, I seem to get a new error. Im really stuck here I am trying to display a table Client which has Columns ID,Name,Phone,Email. I can't seem to get the data into the table. Can anyone help using mysqli?
<?php
include 'connect.php';
include 'form.php';
echo "<table border=1>";
echo "<tr><th>Id</th><th>Name</th><th>Phone</th><th>Email</th></tr>";
if($result = $mysqli_query("SELECT * from Client")){
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row["ID"];
echo "</td><td>";
echo $row["name"];
echo "</td><td>";
echo $row["email"];
echo "</td><td><a href=delclient.php?id=";
echo $row["id"];
echo ">DEL</a> ";
echo "<a href=addclient.php?id=";
echo $row["id"];
echo ">EDIT</a>";
echo "</td></tr>";
}
echo "</table>";
}
?>
Instead of $mysqli_query, it should be mysqli_query:
if($result = mysqli_query("SELECT * FROM Client")) {
As Tristan points out, your call $mysqli_query() is wrong; it should be either
$mysqli->query("SELECT * from Client")
or
mysqli_query($conn, "SELECT * from Client")
where $conn is the database connection created in connect.php. Given your use of object syntax to reference the result fields, I suspect the first of the above to be the correct one.
I am currently working on a PHP and SQLite application. It does have HTML for the webpages being created. I am currently using PDO to connect the database to my online server. When I get to the webpage that allows me to type in what I am searching for then it will display what I have found in the echo statements below. I want to be able to have just the item name, that acts as a hyperlink; when it is clicked on it will go to another webpage (I believe) that will display the item's name, amount, and a short description. Is there a way to use PHP for this action or should I go with the HTML tagging?
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "<td>Amount</td>";
echo "<td>Detail</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
echo "<tr>";
echo "<td>$row[Name]</td>";
echo "<td>$row[Amount]</td>";
echo "<td>$row[Detail]</td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
from what i understand
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
$name = $row['Name'];
echo "<tr>";
echo "<td><a href='details.php?name={$name}'>{$name}</a></td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
To use Associative Arrays within double quotes, you need to use curly braces:
echo "<td>{$row['Name']}</td><td>{$row['Amount']}</td><td>{$row['Detail']}</td>";
Back with another quick question. I have this code below which echo's out product names from a database. What I want to do is make the echoed out product names a link to another page called product.php, each link needs to have a unique ID, for example
Product Name
How would I go about doing this? Many thanks. I will point out that I am very new to PHP.
<?php
//create an ADO connection and open the database
$conn = new COM("ADODB.Connection");
$conn->open("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\WebData\Northwind.mdb");
//execute an SQL statement and return a recordset
$rs = $conn->execute("SELECT product_name FROM Products");
$num_columns = $rs->Fields->Count();
echo "<table border='1'>";
echo "<tr><th>Name</th></tr>";
while (!$rs->EOF) //looping through the recordset (until End Of File)
{
echo "<tr>";
for ($i=0; $i < $num_columns; $i++) {
echo "<td>" . $rs->Fields($i)->value . "</td>";
}
echo "</tr>";
$rs->MoveNext();
}
echo "</table>";
//close the recordset and the database connection
$rs->close();
$rs = null;
$conn->close();
$conn = null;
?>
Assuming your Products table has a unique ID field called "id", change your select to:
$rs = $conn->execute("SELECT id, product_name FROM Products");
And when you want to create a link, use that field and pass it into the URL. So you'd have product.php?id=<?= $thatIdField; ?>.
Example code:
echo "<table border='1'>";
echo "<tr><th>Name</th></tr>";
while (!$rs->EOF) //looping through the recordset (until End Of File)
{
echo "<tr>";
for ($i=0; $i < $num_columns; $i++) {
echo "<td>" . $rs->Fields($i)->value . "</td>";
}
echo "</tr>";
$rs->MoveNext();
}
echo "</table>";
Hey, I have been trying to get this pagination class that I am using to be more ajaxy - meaning when I click on the page number like page [2] the data loads, but I want to load in the data without going to a different page (HTTP request in the background, with no page reloads).
Being new to both php and jquery, I am a little unsure on how to achieve this result, especially while using a php class.
This is what the main page looks like by the way:
<?php
$categoryId=$_GET['category'];
echo $categoryId;
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery_page.js"></script>
<?php
//Include the PS_Pagination class
include('ps_pagination.php');
//Connect to mysql db
$conn = mysql_connect('localhost', 'root', 'root');
mysql_select_db('ajax_demo',$conn);
$sql = "select * from explore where category='$categoryId'";
//Create a PS_Pagination object
$pager = new PS_Pagination($conn, $sql, 3, 11, 'param1=value1¶m2=value2');
//The paginate() function returns a mysql
//result set for the current page
$rs = $pager->paginate();
//Loop through the result set
echo "<table width='800px'>";
while($row = mysql_fetch_assoc($rs)) {
echo "<tr>";
echo"<td>";
echo $row['id'];
echo"</td>";
echo"<td>";
echo $row['site_description'];
echo"</td>";
echo"<td>";
echo $row['site_price'];
echo"</td>";
echo "</tr>";
}
echo "</table>";
echo "<ul id='pagination'>";
echo "<li>";
//Display the navigation
echo $pager->renderFullNav();
echo "</li>";
echo "</ul>";
?>
<div id="loading" ></div>
<div id="content" ></div>
Would I need to do something with this part of the class?, as seen above:
$pager = new PS_Pagination($conn, $sql, 3, 11, 'param1=value1¶m2=value2');
Or this?:
echo $pager->renderFullNav();
I don't no much about jquery,but i guess I would start it like:
$("#pagination li").click(function() {
Then load something maybe...
I don't no. Any help on this would be great. Thanks.
Im not sure how to go about it using that class, it seems it would be a bit tricky, as the script you make the ajax call to, to retrieve the data, will need to have access to the current PS_pagination instance.
Without the class though, it wouldnt be too tricky.
You would need a php script to actually return the data, which takes in the number of records per page, and the current page number. In this script, rather than returning the data, i return the html. So i take the data from the database, then generate the table. This means that all i have to do on success of ajax is replace what is in the able currently, with the new html that i get from this script. Heres an example..
//Current Page Number
$page_num = isset($_GET['page_number']) ? mysql_real_escape_string($_GET['page_number']) : 1;
//Number of records to show on each page
$num_records = isset($_GET['num_records_pp']) ? mysql_real_escape_string($_GET['num_records_pp']) : 10;
//Row to start collecting data from
$start_row = $num_records * ($page_num - 1);
//String to store html to return
$return_html = '';
//SQL Query
$sql = mysql_query("SELECT * FROM my_table LIMIT $start_row, $num_records");
//Query success
if($sql) {
//Construct html for table
$return_html = "<table width='800px'>";
while($row = mysql_fetch_array($sql) {
$return_html .= "<tr>";
$return_html .= "<td>" . $row['id'] . "</td>";
$return_html .= "<td>" . $row['site_description'] . "</td>";
$return_html .= "<td>" . $row['site_price'] . "</td>";
$return_html .= "</tr>";
}
$return_html .= "</table>";
//Query Failed
} else {
$return_html = "<p class='error'>Error Fetching Data</p>";
}
return $return_html;
Then you just make a get request via ajax and pass the page number, and the number of rows you want.
$.get("get_data.php", { page_number: 1, num_records_pp: 20 },
function(data){
$('div#my_table').html(data);
});
So, this query assumses that you have a div with an id of "my_table" which contains your table, it will then replace this with a new table consistion of just the data you requested.
This code was just to give you the jist, so i may have some errors in there, but hope it helps.