I am trying to add a page counter to each classified Ad just like to one on ebay for example. There is a page counter that increases whenever a user clicks on an item.
Here is why I have a problem:
itemlist.php
$query="SELECT post_id, title FROM md_post WHERE category='cars' ORDER BY timeStamp desc";
$result= mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result)){
echo "<a href='itemdescription.php?id=".$row['post_id']."'>". $row['title']."</a>";
so When I click on an item title with id of 3, i would be direction to the itemdescription.php?id=3
My problem is when I add a page counter to itemdescription.php, I get a counter that increments even when I click on different items.
the counter will be 1 for itemdescription.php?id=3
and 2 for itemdescription.php?id=4
I want each ad to have a stand-alone page counter
UPDATE:
Sorry I did not include the code for itemdescription.php page since there is no much in it. Here it is:
<div id="content">
<?php
$query="SELECT name, email, category, region, city, title, price, description FROM md_post WHERE category='cars' AND post_id='$_GET[id]'";
$result= mysql_query($query) or die (mysql_error());
$row = mysql_fetch_array($result);
echo $row['category'] . " in ". $row['city'];
echo $row['title']. " - price is -". $row['price']."$"." ". "<br /><br />";
echo "description"."<br /><br />";
echo $row['description'];
echo $row['name'] . " - ". $row['email']. " region:". $row['region'] . "<br /><br />";
This page is only displaying the ad. e.g. displaying info like price, title, city ...etc
I want to include a stand-alone page counter at the bottom of this page, but when I tried, the counter was not unique for a specific item/ad. It was shared by all items/ads
$_SERVER['REQUEST_URI']
gives you the URL with parameters. you need to update your counter unique to that, not without parameters. if you share more of your codes, we can help you with integration.
I solved it. Thanks for your help anyway. This code below will create a text file that checks the id of the url and counts hits based on the unique ad id. This will prevent mixing counting ads or posts.
<p> this ad has this number of visits </p>
<?php
//--------------------Page counter-------------------------
$stored = "counters/".$_GET['id'].".txt";
function displayHitThingy($stored) {
$fp = #fopen($stored,"r");
$stuff = #fgets($fp,9999);
#fclose($fp);
$fp = fopen($stored,"w");
$stuff += 1;
print "$stuff";
fputs($fp, $stuff);
fclose($fp);
}
displayHitThingy($stored);
//________________________________________________________________________
?>
Related
Ok so eventually I will have let's say 100 products in mysql database. The product page pulls all info from database (such as partnumber, material, color, etc...) and inputs it into the areas of the page that I designate it, all this using php. The previous page will show all 100 products and when user click's on one product it'll go to that product page. Just like all websites right...
I don't believe I need to make 100 individual html pages for each product right? Can I make just 1 main html page that is the templet for the 100 different products? Basically when user clicks the image tag of the product(1st example of code) it'll open the main html templet but somehow call to the database on open and load that specific info? Then other products will have the same process but for they're specific data from database. The 1st sample code is one product on the page that'll display all 100 products with the href containing the image that'll get clicked to show user actual product page retrieved dynamically without page reload, into a predestined section. I'm sure there is a name for what I'm looking to do but all the research I've done I haven't found what I'm looking for. Is there a better way then what I'm explaining? Also I hope this makes sense, Thank you for any input.
<td><img class="td-Image" src="image.jpg">
</td>
<td class="td-manufacturer">
<h6>MANUFACTURER</h6>
<p>Lowes</p>
</td>
<td class="td-addComponent">
<p>$104.99</p>
<button class="add-button">ADD</button>
</td>
<td class="td-material">
<h6>MATERIAL</h6>
<p>Aluminum 7075-t6 Forged</p>
</td>
<td class="td-platform">
<h6>PLATFORM</h6>
<p>Large</p>
</td>
<td class="td-america">
<h6>AMERICAN MADE</h6>
<p>YES</p>
</td>
Actual product page where php gets info from database example
<?php
$sql = "SELECT * FROM Parts;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<div class="description">
<h3>Descrption</h3>
<p>
<?php
echo $row['Description'];
?>
</p>
</div>
<?php
}
}
?>
Editor Note: I edited the question to reflect what he want based on thread on my answer below.
In this scenario you would need to pass in a unique identifier i.e product-id and create a query to fetch from the database product info by product-id
$product-id= $_GET['id'];
$strSQL = "SELECT * FROM AR15Parts WHERE id='$product-id'";
$rs = mysql_query($strSQL);
if (!$rs) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_assoc($rs);
//display your data
if($row){
echo $row['field'];
}
Add an unique Id to your products in your mysql database, using the Auto Increment option (A_I checkbox in phpmyadmin). Then you can pass that id into a link to the product page ```href=“individualProduct.php?id=” while rendering all the products on the all products page.
Then in individualProduct.php you can get that id and retrieve the data
$sql = SELECT * FROM Parts WHERE id =?”;
$stmt = mysqli_prepare($sql);
$stmt->bind_param($_GET[“id”]);
$stmt->execute();
$result = $stmt->get_result();
// Do stuff with $result as it is the individual product corresponding to that id
Optimally, you'll need 2 files:
index/list of products
detail information of the selected product
index files (maybe call it index.php)
Here, you need to select products and make it a list:
$stmt = $pdo->query("SELECT * FROM Parts");
while ($row = $stmt->fetch()) {
echo '' . $row['name']."<br />\n";
}
Since you want the detail to be shown to the index/list page, let's add a container area:
<div id="container-detail">
</div>
Add a little javascript code to handle AJAX request:
<script type="text/javascript">
function loadDetail(itemId){
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://website.address/path/to/detail.php?id=" + itemId, true);
xhr.onreadystatechange = function ()
{
if (xhr.readyState==4 && xhr.status==200)
{
document.getElementById("container-detail").innerHTML=xhr.responseText;
}
}
xhr.send();
}
</script>
detail page (let's call it detail.php)
In this screen, we fetch details for only one part, specified by HTTP GET id parameter. The one that's being supplied from index.php (the ?id= part).
$stmt = $pdo->query("SELECT * FROM Parts WHERE id = '" . $_GET['id'] . "'");
$part = $stmt->fetch();
echo "Name: " . $part['name'] . "<br />\n";
echo "Manufacturer: " . $part['manufacturer'] . "<br />\n";
echo "Price: " . $part['price'] . "<br />\n";
That's it, you should get it working with a few adjustments based on the table and template you have.
A little note is that I used PDO mechanism to do the query. This is because mysql_* function has been deprecated for so long that it is now removed from PHP 7.0. Also, the current PHP version has been PHP 8. So, be prepared, a lot of web hosting might gonna remove older PHP versions, moving forward.
Hi I was wondering if anyone could help. I have just developed a search bar for my site which will be on every page loaded using an include function so they will all be the same. The code can read my database perfectly but I am at a lose on how to send more than one result to another page in the format that I need. The problem is that I post 2 to 3 variables to the next page in the url for each link of this sort and if the search bar returns more than 1 result then each result will need 2 to 3 variables to populate the next page.
Link example
Movies Home
Here is the search bar code.
<?php
if(isset($_POST['search_term'])){
$search_term = $_POST['search_term'];
if (!empty($search_term)) {
$query = "SELECT title FROM database WHERE title LIKE '%".mysql_real_escape_string($search_term)."%'";
$query_run = mysql_query($query);
$query_num_rows = mysql_num_rows($query_run);
$result = mysql_query($query_num_rows);
if ($query_num_rows >= 1) {
echo $query_num_rows.' results found:<br>';
while ($query_row = mysql_fetch_assoc($query_run)){
echo $query_row ['title']. '<br>';
}
}
else{
echo 'No results found.';
}
}
}
?>
This code currently echos on the current page. I am hopeing to pass it to a results page and populate multiple items depending on how many results were found in the search bar. Here is an example of the code that I would be hopeing to populate from the search bar on the target page.
<?php
if (isset($var1)){
$subject_set = mysql_query("Select * FROM database WHERE genre like '%".$info."%' and media = '".$med."' ORDER BY ".$sort." ", $connection);}
else{
$subject_set = mysql_query("Select * FROM media", $connection);
}
if (!$subject_set){
die("Database connection failed: " . mysql_error());
}
htlm code</div>
<?php } ?>
I was thinking that if I was even able to pass the results id's using an array and then retrieve the corrosponding results from the database on the target page.
Sorry I am still quite a novice at this type of coding and I hope I did not confuse anyone with what I am trying to say. Thank you for your time and hopefully I can get this problem sorted. Thanks again.
If you want to display the results on other pages, consider putting them in SESSION variables.
<?php
session_start ( ); // at top of page
...
$theIndex=0;
while ($query_row = mysql_fetch_assoc($query_run)){
echo $query_row ['title']. '<br>';
$_SESSION['searchResult'][$theIndex] =$query_row ['title'];
$theIndex++;
}
Then on your the page where you want to display the results, loop through the SESSION['searchResult'] and echo it out...
<?php
session_start ( ); // at top of page
$theResults = $_SESSION['searchResult'];
foreach ($theResults as $key=>$value){
echo htmlentities($value) . "<br>";
}
I asked this question before but no one could help me unfort. I have images and headings coming from database now the problem is that only one image is displaying(the last image) i need both to display.
here is my revised code
$query = "SELECT page_title, page_image FROM pages WHERE id='$page'";
$result = mysqli_query($connection, $query);
confirm_query($result);
while ($page_fetch = mysqli_fetch_assoc($result)) {
$page_title = $page_fetch['page_title'];
$images = $page_fetch['page_image'];
echo "<div class=\"content \">";
echo "<h3 class=\"words\"> $page_title </h3>";
echo "<img src='pics/" . $images . "' width=\"340\" height=\"252\" alt=\"\" />";
echo "</div>"; //end box
} // close while loop
here is my database for pages
page_id id page_image page_title
1 1 ocean.jpg have a look at the ocean
2 1 house.jpg The house
just some extra info the images must display dynamically , as they coming in from a form to db to this page
Your are returning your images from your query.
Check the view source page, You will find your error their.
Your returing image name will not match with the image which you got in the folder. (For the second one)
im trying to pass a session between 2 div on the same page.
i have links, when i click on one i want the id to pass to the other div to populate it with the info from the link, but it doesnt pass!!
this is the code from my links div
$q =mysqli_query($link, "SELECT * FROM products WHERE status = 1 ORDER BY id DESC");
while($row = mysqli_fetch_array($q)){
$data = $row['image'];
$file = substr($data, strpos($data, "/") + 1);
$_SESSION['id']=$row['id'];
echo"<div class='homedogs'>",
"<a href='merchandise.php' class='productchoice'>",
"<img class='nailthumb-container3' src='$file' alt='{$row['name']}. Image' />",
"</a>",
"<br />",
'NAME: ',$row['name'],"<br />",'PRICE: ',$row['price'],
"</div>";
}
}
and this is other div that i want to use the session in
include 'inc/connect.php';
$q = mysqli_query($link, "SELECT * FROM products WHERE id = '".$_SESSION['id']."'") or
die (mysql_error());
while($row = mysqli_fetch_array($q)){
$data = $row['image'];
$file = substr($data, strpos($data, "/") + 1);
echo"<div class='rehomediv'>",
"<img class='nailthumb-container2' src='$file' alt='{$row['name']}. Image' />","<br
/>",
"<div class='nameagesex'>",
"<div class='item_name'>{$row['name']}</div>",
"<br />",
"<span class='item_price'>{$row['price']}</span>",
"</div>",
"<div class='description'>",
nl2br($row['description']),
"</div>",
</div>;
im sure its simple enought but i cant get it! can anyone help?
thanks
EDIT!!!!!
on page load, pid is not set so i get an error, is there anyway to have it that if pid is not set then it just display the last record?
for anyone confused by this edit, check the accepted answer..
You are assigning $_SESSION['id'] multiple times inside a while loop. This will end up keeping only the last value, provided that you do session_start(), as you claim you do.
If you want this to work, don't use $_SESSION, but a GET query instead. Change your link-producing code to:
"<a href='merchandise.php?pid={$row['id']}' class='productchoice'>"
...and, in merchandise.php check $_GET['pid'] to determine the product id being requested:
if(isset($_GET['pid']))
// show corresponding product (your second listing)
else
// show something else, i.e. the product catalog (your first listing)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
Okay, so I have my page "List Customers" which then links to "List Jobs" adding on ?custID= into the URL, then on List jobs I have used $_GET["custID"] so my query uses the custID from the URL. that works fine, lists out my jobs for said customer using their ID.
My problem comes up now, I need the links on this page to give my third page the techID as well. But the techID is in jobDetails, so my query cannot give them the techID as it is querying my job section.
This is my "ListJobs.php" page, which upon choosing a customer in the page before it, loads this page with the url ListJobs.php?custID=001 (001 being an example, it will give the number depending on your choice of customer).
<?php
// Get data from the database depending on the value of the id in the URL
mysql_select_db($database_con_sim5, $con_sim5);
$query_Recordset1 = "SELECT * FROM job WHERE custID=" . $_GET["custID"] ;
$Rs1 = mysql_query($query_Recordset1);
// Loop the recordset $rs
while($row = mysql_fetch_array($Rs1)) {
$strName1 = $row['jobID'] . " " . $row['jobDesc'] . " " . $row['computerName'];
$strLink = "<a href = 'jobDetails.php?jobID=".$row['jobID']."'>".$strName1."</a>";
// Write the data of the person
echo "<li>" . $strLink . " </li>";
}
// Close the database connection
mysql_close();
?>
Then on pressing one of the links, link to jobDetails.php?jobID= with the job number.
I am able to show all the details in Job Detail with this, but I also need my Tech Name to appear, Tech Name is not in Job Details, but Tech ID is.
Here is my Job Details page coding :
<?php
// Get data from the database depending on the value of the id in the URL
mysql_select_db($database_con_sim5, $con_sim5);
$query_Recordset1 = "SELECT * FROM jobDetail WHERE jobID=" . $_GET["jobID"] ;
$query_Recordset2 = "SELECT technician.techName FROM technician
WHERE techID=" . $query_Recordset1["techID"] ;
$Rs1 = mysql_query($query_Recordset1);
$Rs2 = mysql_query($query_Recordset2);
while($row1 = mysql_fetch_array($Rs1)) {
while($row2 = mysql_fetch_array($Rs2)) {
echo "<dt><strong>Job Note ID:</strong></dt><dd>".$row1["jobNoteID"]."</dd>";
echo "<dt><strong>Job Notes:</strong></dt><dd>".$row1["jobNotes"]."</dd>";
echo "<dt><strong>Date Completed:</strong></dt><dd>".$row1["dateCompleted"]."</dd>";
echo "<dt><strong>Time Spent:</strong></dt><dd>".$row1["timeSpent"]."</dd>";
echo "<dt><strong>Job ID:</strong></dt><dd>".$row1["jobID"]."</dd>";
echo "<dt><strong>Technician ID:</strong></dt><dd>".$row1["techID"]."</dd>";
echo "<dt><strong>Technician Name:</strong></dt><dd>".$row2["techName"]."</dd>";
}
}
// Close the database connection
mysql_close();
?>
The error I am getting is:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\Sim5Server\Pages\jobDetails.php on line 129
That line is:
while($row2 = mysql_fetch_array($Rs2)) {
I hope I am making any sense AT ALL.
To sum up, I need my final page to show data from mysql technician using the Primary/index key techID.
Some way to add techID onto either listJobs' links to job details or in job details' second Recordset.
EDIT:
I should probably state this will never be used on the net, I only need it to work for an assignment. in future, thanks to a comment, I will no longer be using mysql_* I am jsut using them as my entire workbook tells us to use it.
Try making this change in your jobDetails.php page
$query_Recordset1 = "SELECT * FROM jobDetail WHERE jobID=". (int) $_GET["jobID"];
$Rs1 = mysql_query($query_Recordset1) or die(mysql_error());
while($row1 = mysql_fetch_array($Rs1)) {
$query_Recordset2 = "SELECT technician.techName FROM technician
WHERE techID=" . $row1["techID"] ;
$Rs2 = mysql_query($query_Recordset2) or die(mysql_error());
while($row2 = mysql_fetch_array($Rs2)) {
For the second query you were using the result resource from executing the first query as techID. Also you will have to query for techName from inside the while loop fetching job details, since only then will you have the techID.