Trying to use Jquery and Ajax to swap out page content - javascript

I need to preface my question with some history so you know what and who you are dealing with. I am a 75 year old just retired house painter who is building just this one website so that I can sell some of the stuff that I have done for many years as a hobby. I have a decent understanding of HTML and CSS and a lesser knowledge of PHP from browsing the internet. JavaScript knowledge is zilch.
This is an include file for the content in one of the sections of my website. It works fine with just one Mysqli query and no JavaScript, but with a lot of products to list I would like to swap the content so I wouldn't have to add more pages. The JavaScript is borrowed from a tutorial by Adam Khoury and I just plugged in my information to it.
My question is, can I use the LIMIT clause as a mechanism to swap the content of the page as I tried within the code below? At this point I am getting this error.
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result,
boolean given in C:\wamp\www\elkcreek\includes\cabscontent1.php on line 37.
This line is:
while ($row = mysqli_fetch_assoc($result))
Thanks in advance for any help or just for reading this diatribe.
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
?>
<?php
require "scripts/connect_to_copy.php";
$contentVar = '';
$products = '';
$sql = '';
if (isset($_POST['contentVar'])) {$contentVar = $_POST['contentVar'];}
if ($contentVar == "con1")
{
$sql = mysqli_query($link, "SELECT * FROM cabs ORDER BY id LIMIT 0, 4" );
}
else if ($contentVar == "con2")
{
$sql = mysqli_query($link, "SELECT * FROM cabs ORDER BY id LIMIT 4, 8");
}
else if ($contentVar == "con3")
{
$sql = mysqli_query($link, "SELECT * FROM cabs ORDER BY id LIMIT 8, 12");
}
$result = mysql_query($sql)
while ($row = mysqli_fetch_assoc($result))
{
$pages = $row["pages"];
$left_image = $row["left_image"];
$product_name = $row["product_name"];
$price = $row["price"];
$ordering_number = $row["ordering_number"];
$sold = $row["sold"];
$products .=
'<div class="product">
<div class="product_pictures"><a href="' . $pages . '">
<img src="' . $left_image . '" alt="cabochon" width="150" height="200"/></a>
</div>
<div class="product_name"><h3>' . $product_name . '</h3></div>
<div class="price">$' . $price . '</div>
<div class="order_number">Order# C' . $ordering_number . '</div>
<div class="sold">' . $sold . '</div>
</div>';
}
?>
<script type="text/javascript" src="jquery-1.10.2.js"></script>
<script>
function swapContent(cv) {
$(".product").html('<img src="loader.gif"/>').show();
var url = "cabscontent1.php?";
$.post(url, {contentVar: cv} ,function(data) {
$(".product").html(data).show();
});
}
</script>
<div class="desc">CLICK ON PICTURES FOR LARGER PICTURES - DESCRIPTIONS
</div>
<div class="more_cabs">MORE CABS
1
2
3
</div>
<div class="product_column"><?php echo $products; ?></div>

Related

I have a single div and it needs to be duplicated dynamically

I have a single div and a unknown number(say n) of rows of data. i need to show that data on my html cards
and these cards are to be generated dynamically by the output value(n).
say if i have 10 rows of data.my div element needs to be created 10 times and each row data is to be displayed on each div
by the way i am using PHP for backend.
here is my codes
This is my div
<div class="row">
<div class="column">
<div class="card">
<h1><?php echo"$value"; ?></h1>
<h3><?php<?php echo"$description";?></h3>
</div>
</div>
</div>
and this is my php code
<?php
$conn=new mysqli("localhost","root","","programmingpioneers");
if(!$conn)
{
echo "connection_failed";
}
else{
//echo "sucess";
}
$query= "select title,description from problems where difficulty='hard'";
$result=mysqli_query($conn,$query);
$row=mysqli_fetch_array($result);
if (mysqli_query($conn, $query))
{
echo "sucess<br>";
while ($row=mysqli_fetch_array($result)) {
$title=$row[0];
$description=$row[1];
echo "$title <br> $description";
}
}
else {
echo "Error: " . $query . "<br>" . mysqli_error($conn);
}
?>
if i try to but my div inside
echo"$title <br>$description";
it is throwing the following error
Parse error: syntax error, unexpected 'row' (T_STRING), expecting ';' or ',' in C:\xampp\htdocs\myapp\useless.php on line 19
Use loop in the div you want to have again and again.Please check the code if it works for you.
enter image description here
enter image description here
Without having all your data available I'm unable to test it, but this should be the correct way of doing it. I left comments in the code and removed a bunch of syntax errors that you made:
<?php
// Create MySQLi object
$conn = new mysqli("localhost","root","","programmingpioneers");
// Verify MySQL connection
if ($conn->connect_errno) {
echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
}
// Query
$query = "SELECT title,description FROM problems WHERE difficulty = 'hard'";
// Run query
if($result = $conn->query($query)) {
// Loop through query results
while ($row = $result->fetch_assoc()){
echo "<div class='row'>";
echo "<div class='column'>";
echo "<div class='card'>";
echo "<h1>". $row['title'] ."</h1>";
echo "<h3>". $row['description'] ."</h3>";
echo "</div></div></div>";
}
// Output query errors
} else {
echo "Failed to select title, description: (" . $conn->errno . ") " . $conn->error;
}
// Close the MySQLi connection
$conn->close();
?>

simplePagination.js not working for php MySQL database search

Used a form to create a php search for a MySQL database in a header.php file.
Attempting to use simplePagination.js with php. I am able to correctly calculate the number of results and display the appropriate amount of page links. However, search.php is not limiting the number of items on the page, and all of the pagination links lead to a blank page.
<form action="search.php" method="POST">
<input type="text" name="search" placeholder="search site">
<button type="submit" name="submit-search"><img src="../assets/search icon-05.png"></button>
</form>
search.php code:
<?php
include 'header.php';
?>
<section class="searchPage">
<div class="searchResults">
<?php
if (isset($_POST['submit-search'])){
$searchTerm = trim( (string) $_POST['search'] );
if (isset( $searchTerm[0] )) {
$search = mysqli_real_escape_string($conn, $_POST['search']);
$sql = "SELECT * FROM articles WHERE title LIKE '%$search%' OR abstract LIKE '%$search%' OR keywords LIKE '%$search%'";
$result = mysqli_query($conn, $sql);
$queryResult = mysqli_num_rows($result);
$limit = 10;
$numberOfPages = ceil($queryResult/$limit);
if ($queryResult > 0){
echo $queryResult . " results found";
while ($row = mysqli_fetch_assoc($result)){
echo "<div class='articleItem'>
<h2>".$row['title']."</h2>
<p>".$row['abstract']."</p>
<a href=".$row['link']." target='_blank'>".$row['link']."</a>
</div>";
}
$pageLinks = "<nav><ul class='pagination'>";
for ($i=1; $i<=$numberOfPages; $i++) {
$pageLinks .= "<li><a href='search.php?page=".$i."'>".$i."</a></li>";
};
echo $pageLinks . "</ul></nav>";
}
else {
echo "There are no results matching your search.";
}
}
}
?>
</div>
</section>
<script type="text/javascript">
$(document).ready(function(){
$('.pagination').pagination({
items: <?php echo $queryResult;?>,
itemsOnPage: <?php echo $limit;?>,
currentPage : <?php echo $page;?>,
hrefTextPrefix : 'search.php?page='
});
});
</script>
You don't need to create the page links on your own, because this is what the plugin does through JavaScript events. So you can replace the ul with a div element. This is the reason why you get a blank page.
echo "<nav><div class='pagination'></div></nav>";
In the following is what I added to make it work:
$(document).ready(function(){
var pageParts = $(".articleItem");
pageParts.slice(<?php echo $limit;?>).hide();
$('.pagination').pagination({
items: <?php echo $queryResult;?>,
itemsOnPage: <?php echo $limit;?>,
onPageClick: function(pageNum) {
var start = <?php echo $limit;?> * (pageNum - 1);
var end = start + <?php echo $limit;?>;
pageParts.hide().slice(start, end).show();
}
});
});

PHP Javascript Array

Hi I'm a novice PHP developer to say the least and I'm pretty stuck.
I'm trying to get the below code working I know I need a foreach loop or similar but I'm way out of my depth, I'm really not sure how to get this working, I know to most of you this is basic stuff but I'm lost.
Basically this is a receipt when a tab is settled. It searches the MySql Database for the relevant items and should print them on the receipt.
I know the MySql Query is not linked to the output at the bottom but I don't know how to do it.
<html lang="en">
<head>
<title>Receipt</title>
</head>
<body>
<?php
$invoicenum = $_POST['invoicenum'];
$name = $_POST['name'];
$netrev=$invoicenum - 1;
?>
Items:
<?
$itemQuery = mysql_query("SELECT * FROM sales WHERE invoicenum = '$netrev' AND tabname = '$name'");
$result = array();
while($row = mysql_fetch_array($itemQuery))
{
$result[] = $row['itemname'];
}
echo json_encode($result);
$amounts = json_decode($result['amounts']);
$items = json_decode($result['items']);
$prices = json_decode($result['prices']);
?>
<br>
<?
for ($i = 0; $i < count($items); $i++)
{
echo $amounts[$i] . "x " . $items[$i] . " - " . $prices[$i] . "<br>";
}
?>
</body>
</html>
I've removed code that isn't relevant to this array, if you can help I'd be forever grateful.
no need of encode and decode.
$itemQuery = mysql_query("SELECT * FROM sales WHERE invoicenum = '$netrev' AND tabname = '$name'");
while($row = mysql_fetch_array($itemQuery))
{
echo $row['amounts'] . "x " . $row['items'] . " - " . $row['prices'] . "<br>";
}
?>
Forget all json_decode/encode after filling $result then:
First do:
$result[] = $row;
Then:
foreach ($result as $set){
echo $set['amount'] . "x " . $set['itemname'] . " - " . $set['price'] . "<br>";
#do var_dump($set); here for checking the keys
}

JQuery Autocomplete Form from Database fails

I'm following a tutorial (http://www.pontikis.net/blog/jquery-ui-autocomplete-step-by-step) and I try to do this but with a local phpmyadmin database, using Xampp installed on my PC.
I have a table called cars with the columns id and name, I filled in 2 entries, however, the autocomplete doesn't work, in fact, I don't get any suggestions below my form. Since I can't bother you with all the JS files, maybe someone can explain what this precisely does, especially the last while function, because I think here lies the problem:
$sql = 'SELECT id, name FROM cars';
for($i = 0; $i < $p; $i++) {
$sql .= ' AND name LIKE ' . "'%" . $conn->real_escape_string($parts[$i]) . "%'";
}
while($row = $rs->fetch_assoc()) {
$a_json_row["id"] = $row['id'];
$a_json_row["value"] = $row['name'];
$a_json_row["label"] = $row['name'];
array_push($a_json, $a_json_row);
}
You are not declaring WHERE in your SQL statement. Query likely failing, yielding zero autocomplete options.
$sql = 'SELECT id, name FROM cars';
for($i = 0; $i < $p; $i++) {
if ($i == 0){ $sql.= " WHERE"; } // THIS LINE HERE
$sql .= ' AND name LIKE ' . "'%" . $conn->real_escape_string($parts[$i]) . "%'";
}
My Solution to this Problem:
while($cars=mysql_fetch_array($query)){
$json[]=array(
'value'=>$cars["name"],
'label'=>$cars["name"]
);
}
echo json_encode($json);

How to get data from php in JS on a native android app?

I'm working on a cross-platform application and I got some troubles with my data.
Actually I have a full website with a lot of php and I'm working with the Intel XDK to make a native application of this website.
But here is the thing, I know I can execute php on my native app, so i'm trying to execute few scripts directly on my server and to take back the result with an ajax request.
Here is the code : (Javascript)
var games = location.search;
var res = games.split("=");
$.ajax({ //create an ajax request to a page that prints the address.
url: "http://tonight-app.fr/php/mobile_app/getGamesLists.php", //url for the address page
data: {"name": res[1]},
success: function(result){
var games = result; //create a variable and give it the value of the response data
var gamesSplit = games.split(";");
for(i=0;i<gamesSplit.length;i++){
var gamesSplit2 = gamesSplit[i].split(",");
test(gamesSplit2[0]);
}
}
});
function test(gamesSplit2) {
console.log(gamesSplit2);
var ul = document.createElement("ul");
ul.id = "email-list";
ul.innerHTML = gamesSplit2;
document.getElementById('test').appendChild(ul);
}
Here is the php on the server (to this address mention in the url of the ajax)
<?php
require_once("connect_database.php");
mysqli_set_charset($con, "utf8");
$name = $_GET["name"];
$sql="SELECT * FROM `games`";
$reponse = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($reponse, MYSQL_NUM)) {
if ($row[2] == $name) {
echo $result = '
<a href="gamesReceipes.php?id=',$row[0],'">
<li class="unread clickable-row">
<div class="name">
',$row[1],'
</div>
<div class="message">
Voir la préparation
</div>
</li>;';
}
}
echo $result;
?>
So to explain, I'm executing my php script which gave me the $result and i'm supposed to display this result in my Ajax.
It's working on the emulator in the Intel XDK but not after when i'm building the app ! (Of course my phone have the 4g activated)
It's supposed to be like this on the display :
[
I hope you can understand my problem here ... Thanks guys !
By this link :
https://software.intel.com/en-us/articles/cordova-whitelisting-with-intel-xdk-for-ajax-and-launching-external-apps
Thanks yo #OldGeeksGuide who gave me this link ! I just had to add the link to my script in the intel xdk and it worked ! Thanks !
You appear to be trying to echo the same thing twice. returning data to an AJAX call should be done once and the last thing you do in the script.
<?php
require_once("connect_database.php");
mysqli_set_charset($con, "utf8");
$name = $_GET["name"];
$sql="SELECT * FROM `games`";
$reponse = mysqli_query($con, $sql);
// init the $result var
$result = '';
// incorrect parameter constant
//while ($row = mysqli_fetch_array($reponse, MYSQL_NUM)) {
while ($row = mysqli_fetch_array($reponse, MYSQLI_NUM)) {
if ($row[2] == $name) {
//echo $result = '
$result .= '
<a href="gamesReceipes.php?id=' . $row[0] . '">
<li class="unread clickable-row">
<div class="name">
' . $row[1] . '
</div>
<div class="message">
Voir la préparation
</div>
</li>;';
}
}
echo $result;
?>
You could also simplify this.
As you are passing the name of the game to this script you could add that to the query as a search criteria like so and then remove a lot of unnecessary processing.
<?php
require_once("connect_database.php");
mysqli_set_charset($con, "utf8");
// init the $result var
$result = '';
if ( isset($_GET['name'] ) {
$name = $_GET["name"];
$sql="SELECT * FROM `games` WHERE `name` = '$name'";
$reponse = mysqli_query($con, $sql);
// I assume there is only one ro w that contains this name
// so the loop is not required now
while ($row = mysqli_fetch_array($reponse, MYSQLI_NUM)) {
$result .= '
<a href="gamesReceipes.php?id=' . $row[0] . '">
<li class="unread clickable-row">
<div class="name">' . $row[1] . '</div>
<div class="message">Voir la préparation</div>
</li>;';
}
} else {
$result = 'No name parameter passed';
}
echo $result;
exit;
?>

Categories

Resources