I have around 10000 records in a mysql table which has id, info and ip columns. ip columns are empty. I want to display one row at a time whenever the page refreshes. For every row displayed i want to store the IP of the client refreshing the page. So whenever the page is refreshed from that IP it displays same info.
I hope i explained it well. How can I make sure that it displays correct row everytime page is refreshed and i don't make extra mysql queries.
Currently I am fetching up all the rows every time person opens the page and then filtering it but I know this is not efficient. Please help
I hope understand you. So, you want show the same row to a IP that it already saved:
// Controller...
$ip = $container->get('request')->getClientIp(); // The ip
$em = $this->getDoctrine()->getManager(); // Get the Entity Manager
// First check if the ip has assigned a record
$record = $em->getRepository('AcmeStoreBundle:Records')
->findOneByIp( $ip );
if( !$record ){ // There isn't a IP assigned
$record = $em->getRepository('AcmeStoreBundle:Records')
->findOneBy( array('ip' => null) ); // Get other record to assign
$record->setIp( $ip );
$em->flush(); // Save the ip
}
return new Response('Record info: ' . $record->getInfo() );
The first time you get a record doesn't used, after you get the same record for your IP.
Use the iterate() method together with batch processing.
http://docs.doctrine-project.org/en/2.0.x/reference/batch-processing.html
If you need only a subset of the records, you can use setFirstResult($num) and setMaxResults($num).
Related
I have a website where it displays all of my records. I can click on an individual record and it gets the student_id of that record and updates it to the URL eg. view_student.php?id=12.
It then takes me to a new page where I want it to display all the information about that record, in this case, show all information about student number 12, but none else.
I haven't a clue how to carry out the statement to display all of the information for that record, this is what I have so far:
if (isset($_GET['student_id'])) {
echo $row['student_name'] . $row['student_age'] . $row['student_gender'];
}
This is a standalone page with nothing else on it. view_student.php simply uses a require function to this script. This code does not display anything, nor does it display any errors. I'm using PDO and I have made sure I'm connected to the database.
My guess is that I will need to use a WHERE clause but I'm just not too sure
Thank you
You can use below PDO query to fetch your data
$statement = $db_con->prepare("select * from student where student_id = :student_id");
$statement->execute(array(':student_id' => $_GET['student_id']));
$row = $statement->fetchAll(PDO::FETCH_ASSOC);
I need my visitors on homepage to see how many active user we have. I need to include this with php. (need the code)
in mysql database user is marked with 0 (inactive) and 1 (active).
How can I count/read out the outcome, means all active users, of the database(database_name) - table(user_registration) - colums(each_one) - count(status)?
How to publish this on my website?
Would prefer to this in php cause i'm not so used to js and query.
A simple methode/explanation would be very nice.
Thank you.
(need the code) first this is not a script writing service. Come up with some try and we will try giving a solution.
Basic necessity for your required active user count is to have a status (or name it something) column in your user table.
Then, while a user logs in change the status from 0 to 1 while logs out change it from 1 to 0
by doing so you could easily query it to display the active user.
select column,column from table where status = 1
//(i.e)
select id,userName from users where status = 1
Try using ajax if you want to display it like on-line users displayed in fb
In our implementation, I created a separate DB table active_users, wrote a php function to write user_id and timestamp to this table. This function is called via ajax from your page, bind it to whatever user action you see fit (in our case it is a search).
Write another php function that queries the said tables and retrieves users with time difference between the timestamp and now less than specific value (in our case 5 min). call this function from your page via ajax either in specific time intervals (heartbeat) or user action again - mouse move etc. Display results of this call somewhere (in our case "Active Users" div)
this is what i have.
it works good, but it just shows the amount of lines within.
How can i count all numbers in 'status' within those lines?
<?php
$con = mysql_connect($dbHost, $dbUser, $dbPass );
mysql_select_db($dbName, $con);
$res = mysql_query('SELECT Count(*) FROM ' . 'user_registration', $con);
if ($row = mysql_fetch_array($res, MYSQL_NUM))
{
$users = trim($row[0]);
}
else
{
$users = 'Error';
}
?>
to Show result on website:
<?= $users ?>
How can I get back (to the browser) some data automatically (from server) after a POST?
I have a HTML form
I POST the form's data to a php code eg: saveRecord()
the php code saves the data in a mySQL DB
if the data contains a non 0 record ID there will be an UPDATE, if the record ID == 0 there will be an INSERT, and the php code knows (gets back from DB) the new record ID
(after INSTER) how can I send that new record ID back to (browser) HTML form?
If I edit an existing record (ID !=0 ), everithing is fine...
My problem is that, when I post NEW data (ID == 0) I can post it several times, and the php code each time creates a new record in the DB... But I really don't want that. I would like to send (from the server) back (to the browser) the INSERTed records ID (right in the HTML form), to prevent multiple INSERTs...
How can I do that?!
the specific CI code:
function update($record) {
print_r($record);
$id = $record['crn_id'];
$record['crn_active'] = ( ($record['crn_active'] == 'on') ? 1 : 0 );
array_shift($record);
if ($id == 0) {
$this->db->insert('currencies', $record);
} else {
$this->db->where('crn_id', $id);
$this->db->update('currencies', $record);
}
}
?>
Here are three ways to help you on your way that can be generalized:
Make another column unique. If you care so much about possibly having duplicate data in your database there may be some rule to it that you can generalize in the form of a constraint.
Disable the submit button after it was clicked (e.g. <input type="submit" onsubmit="this.disabled=true">). (make sure to re-enable it if some ajax based validation fails)
Use a one time key generated to make sure the user did not just reload the submitted page. Have a look at form keys which can also help against CSRF and XSS attacks.
I have this PHP page where the user can select and un-select items. The interface looks like this:
Now I'm using these code, when the user hit the save changes button:
foreach( $value as $al_id ){ //al_id is actually location id
//check if a record exists
//if location were assigned and leave it as is
$assigned_count = $this->AssignedLoc->checkIfAssigned( $tab_user_id, $al_id );
if( $assigned_count == 0 ){
//else if not, insert this new record
$this->insertAssigned( $tab_user_id, $company_id, $al_id );
}
}
Now my question is, how do I delete the un assigned locations? For example in the screenshot above, there are 4 assigned locations, if I'm gonna remove (or unassign) "Mercury Morong" and "GP Hagonoy" from the assigned locations, only two must remain. What are the possible solutions using PHP?
Thanks for any help!
Loop through the submitted values in the unavailable selection. If a row with that id/name/whatever identifier you use exists in the "assigned" section, then delete that row from the assigned section. Use you pre-existing checkIfAssigned function.
I am allowing users to vote on content, so I need to save a user input to my database, and then get a COUNT() of the input to return back.
The voting works fine, but reading the results back always returns false. The only work around I have found is to rebuild the database connection a second time to count the votes. Is there any other way to do this?
Here is my code:
$vote = $conn->prepare($voteSQL);
$vote->execute(array(':postId'=>$voteId
, ':voterId'=>$userId
, ':voteType'=> $voteDir
,':voteType2'=> $voteDir
,':voteType3'=> $voteDir));
$tallyVotes = $conn->prepare($tallySQL);
$tallyVotes->execute(array(':postId'=>$voteId));
$updatedTally = $tallyVotes->fetch();