3

I have a php script that displays records from a database. It's probably not the best script, as I'm very new to php.

I've added an additional column in my table and would like to keep a count in that column to show me how many times each of the records have been viewed.

Heres the part of the code I think i need to add the code to... if i need to post the entire page i will, but i just figured i could add the line to this part.

//Get the details from previous page
$SelectedCounty = $_POST["result"];

//set variable for next SEARCH
$option = '';

// Get the county names from database - no duplicates - Order A-Z 
$query = "SELECT DISTINCT tradingCounty FROM offers ORDER BY tradingCounty ASC";

// execute the query, $result will hold all of the Counties in an array
$result = mysqli_query($con,$query);


 while($row = mysqli_fetch_array($result)) {
$option .="<option>" . $row['tradingCounty'] . "</option>";

}
    }

the new column name is 'views' and i just want to add 1 to it each time a record from the database is viewed.

any help greatly appreciated.

1
  • A couple of answers have explained how to do this. Be aware that there's no built-in magic in MySQL that will increment your views column automatically whenever the row is retrieved; you need to program this explicitly. Commented Dec 3, 2014 at 13:56

5 Answers 5

6

Add a new field views to the table.

When, user views the page, fire the SQL.

$query = "UPDATE offers SET views = views + 1";
Sign up to request clarification or add additional context in comments.

1 Comment

Table is actually offers
2
mysqli_query($con,"update offers set views = views + 1");

2 Comments

Are you sure you didn't just copy Programming Students answer?
This is a 2+2 type of question, don't think he copied it. Obvious one
0

If you have added the column, it probably has a NULL value. Either set the value to 0, by doing:

update offers
    set views = 0;

Or use:

update offers
    set views = coalesce(views, 0) + 1;

Comments

0

You can change your code with this rewritten code assuming that your Table has a column views (datatype int).

//Get the details from previous page
$SelectedCounty = $_POST["result"];

//set variable for next SEARCH
$option = '';

// Get the county names from database - no duplicates - Order A-Z 
$query = "SELECT DISTINCT tradingCounty FROM offers ORDER BY tradingCounty ASC";

// execute the query, $result will hold all of the Counties in an array
$result = mysqli_query($con,$query);

if($result){
$query2 = "UPDATE offers SET views=views+1;
mysqli_query($con,$query2); 
}



 while($row = mysqli_fetch_array($result)) {
 $option .="<option>" . $row['tradingCounty'] . "</option>";

 }

Or if you need to track the view counts for individual records, you need to modify your code a bit. And probably you need to add one more field in the database for eg. id (datatype int) which can distinguish between different records.

Please clear your problem properly.

3 Comments

Hi, Yes I need to track the views for each record... At the moment the best i've been able to achieve is to count each record that is shown and place the total of all of them in each records 'views' column, meaning my last test I ended up with the figure '310' listed as the view count for each record. here's the code I've used so far...
Hi, Yes I need to track the views for each record... At the moment the best i've been able to achieve is to count each record that is shown and place the total of all of them in each records 'views' column, meaning my last test I ended up with the figure '310' listed as the view count for each record. here's the code I've used so far... while($row = mysqli_fetch_row($result)) { mysqli_query($con,"UPDATE offers SET views = views + 1"); echo ("<div..... }
hi sturobinson81, please let me know the type of program you are trying to make. Like, you want the total count for each records rite, but how what does your application has, does that have number of articles for example, when someone clicks that you wanna add the +1 entry in the views section or its something else. Kindly elaborate.
0

As far as i have analysed your code it brings out the following case.

There are different records for tradingConty, and whenever a user views that particular record(one of the tradingCounty record) by clicking that or any other action specified, the php script is set to increament the view count for that particular entry(we can get that by id) in the database.

If thats the scenario, we can easily generate a code accordingly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.