Creating a Follow User mechanism - twitter-follow

I am trying to create a follow mechanism, where in a user can follow the other. What I am doing is if A tries to follow B, I insert A email id against B in follow table. Now when A again tries to follow B, A id is checked against B, if found true, it redirects and shows already followed but if A tries to follow someone whom A hasnt, it adds them.
Now the problem is, the first part is working fine, but if I follow the same person again, it gets entered again in the table.
$temp=$_GET['temp'];
echo $temp;
mysql_connect('localhost','root','');
mysql_select_db('reviewed');
$query="SELECT follow_user from follow where my_email= '".$_SESSION['email']."'";
$data=mysql_query($query);
if (!$data) { // add this check.
die('Invalid query: ' . mysql_error()) ;
}
if(mysql_num_rows($data) > 0)
{
while($row=mysql_fetch_array($data))
{
$user=$row['follow_user'];
if($temp===$user)
{
header('Location:acq.php?success=1');
$_SESSION['message'] = 'Already Following.';
}
else
{
mysql_connect('localhost','root','');
mysql_select_db('reviewed');
$query="INSERT INTO follow (my_email, follow_user) VALUES('".$_SESSION['email']."','".$temp."')";
$data=mysql_query($query);
if (!$data) { // add this check.
die('Invalid query: ' . mysql_error()) ;
}
echo "Success";
}
}
}
else
{
mysql_connect('localhost','root','');
mysql_select_db('reviewed');
$query="INSERT INTO follow (my_email, follow_user) VALUES('".$_SESSION['email']."','".$temp."')";
$data=mysql_query($query);
if (!$data) { // add this check.
die('Invalid query: ' . mysql_error()) ;
}
echo "Success";
}

It looks to me as though you are adding a follow record for every user other than B that A is following. I think you might be able to fix this by restricting to B as well as A in your initial query i.e.
$query="SELECT follow_user from follow where my_email= '".$_SESSION['email']."' and follow_user = '".$temp"'";
You can then remove the while loop:
if(mysql_num_rows($data) > 0)
{
header('Location:acq.php?success=1');
$_SESSION['message'] = 'Already Following.';
}
else
{
mysql_connect('localhost','root','');
mysql_select_db('reviewed');
$query="INSERT INTO follow (my_email, follow_user) VALUES('".$_SESSION['email']."','".$temp."')";
$data=mysql_query($query);
if (!$data) { // add this check.
die('Invalid query: ' . mysql_error()) ;
}
echo "Success";
}

Related

if statement is running without the press of submit button in codeigniter

I want my if statement to show insert successful or failed only after I press the submit button, but as soon as I load my function master admin, is shows insert failed. What's wrong in my code.
public function masteradmin(){
$data = $this->session->all_userdata();
$this->load->view('user/layout/header',array('data'=>$data));
$this->load->view('user/layout/left_navbar',array('data'=>$data));
$this->load->view('user/appconfig/masteradmin');
$this->load->view('user/layout/footer');
$post= $this->input->post();
unset ($post['submit']);
$this->load->model('AppconfigModel');
if ($this->AppconfigModel->insert_masteradmin($post))
{
echo "insert successfull";
}
else
{
echo "insert failed";
}`
try this
if form submit and post have value that time only it will execute
$post= $this->input->post();
if($post){
unset ($post['submit']);
$this->load->model('AppconfigModel');
if ($this->AppconfigModel->insert_masteradmin($post))
{
echo "insert successfull";
}
else
{
echo "insert failed";
}
}

How to store MySQLi bind_result value for further processing?

In the below code,I can echo $count in bind_result by using while loop(commented in the code). But instead I want to keep that value in the $count variable for further processing instead of just printing the value. How do I hold the value for $count?
/*......mysqli connection...*/
$stmt =$mysqli->prepare("select count(*) from tab where rtd_id=? ");
$stmt->bind_param('i',$id);
$id = 1;
$stmt->execute();
$stmt->bind_result($count);
/*while($stmt->fetch())
{
echo $count;
}*/
if($count>0)
{
//DO SOMETHING
}
?>
First of all, you don't need a loop here.
$stmt->bind_result($count);
$stmt->fetch();
if($count>0)
{
//DO SOMETHING
}

Filter information and custom repository with Symfony2?

From GET parameters, I want to get information from my entities.
In my view I created a form with 3 selects (not multiple ones), like this:
http://pix.toile-libre.org/upload/original/1393414663.png
If I filter only by user I got this in the url: ?category=0&user=6&status=0
I'll have to handle the 0 values...
This form used to filter my tasks.
This is a part of my action in my controller:
if($request->query->has('user')) {
$category_id = $request->query->get('category');
$user_id = $request->query->get('user');
$status_id = $request->query->get('status');
// A little test to see if it works.
echo $category_id . '<br>' . $user_id . '<br>' . $status_id;
// I will pass these variables to a repository
$tasks = $em->getRepository('LanCrmBundle:Task')->findFiltered($category_id, $user_id, $status_id);
} else {
$tasks = $em->getRepository('LanCrmBundle:Task')->findAll();
}
I create a repository with this method:
public function findFiltered($category_id, $user_id, $status_id)
{
/**
* Get filtered tasks.
*
* Get only title, priority, created_at, category_id, user_id and status_id fields (optimization)
*
* Where field category_id = $category_id unless $category_id is smaller than 1 (not secure enough)
* Where field user_id = $user_id unless $user_id is smaller than 1 (not secure enough)
* Where field status_id = $status_id unless $status_id is smaller than 1 (not secure enough)
* Should I do these tests here or in the controller?
*/
}
How to do this query ? Do you have any other elegant suggestions to solve this problem?
You can try:
public function findFiltered($category_id, $user_id, $status_id)
{
$queryBuilder = $this->createQueryBuilder('t');
if(!empty($category_id) && is_numeric($category_id)) {
$queryBuilder->andWhere('t.category = :category')->setParameter('category', $category_id);
}
if(!empty($user_id) && is_numeric($user_id)) {
$queryBuilder->andWhere('t.user = :user')->setParameter('user', $user_id);
}
if(!empty($status_id) && is_numeric($status_id)) {
$queryBuilder->andWhere('t.status = :status')->setParameter('status', $status_id);
}
return $queryBuilder->getQuery()->getResult();
}

Why is the same date echoing despite being created and stored properly in db?

I am pulling comments from my database. The 'created_on' field I have created is inserting properly, however when I echo the results, it's a random date and time that is the same no matter what(Mar 11, 2013 at 10:50 AM).
Here is my query to pull the records:
public function get_airwave_comments($profile_id)
{
$session = $this->session->userdata('is_logged_in');
$user_id= $this->session->userdata('id');
if($profile_id == $user_id)
{
$comments_query = "SELECT * FROM airwaves_comments aw,users u WHERE u.id=aw.from_id AND AW.FROM_id=aw.to_id AND aw.from_id=".$profile_id." order by aw.created_on desc" ;
}
else
{
$comments_query = "SELECT * FROM airwaves_comments aw,users u WHERE u.id=aw.from_id AND aw.to_id=".$profile_id." order by aw.created_on desc" ;
}
$query = $this->db->query($comments_query);
if($query->num_rows() >= 1)
{
$data = $query->result_array();
// return whole resultset. It contains zero, one or more records
return $data;
}
else return false;
$query = $this->db->query($poster_info_query);
if($query->num_rows() >= 1)
{
$data = $query->result_array();
// return whole resultset. It contains zero, one or more records
return $data;
}
else return false;
}
}
Here is the view in which I'm trying to echo the 'created_on' field properly:
if ($airwave && $profile_id == $session_id)
{
foreach ($airwave as $airwave_comment_row)
$airwave_comment_row = $airwave[0];
{
echo "<div id='profile_airwave'>";
echo $airwave_comment_row['comment'];
echo "<br />";
echo "<span class='profile_airwave_info'>";
echo date('M d, Y',strtotime($airwave_comment_row['created_on'])); ?> at <?php echo date('g:i A',strtotime($airwave_comment_row['created_on']));
echo "</span>";
echo "</div>";
}
}
so basically even if I just do echo $airwave_comment_row['created_on']; It's echoing the same said date.
thanks in advance.
I changed the name of the datetime column in the comments table so that it was different from the datetime column in the users table it's being spliced with.

Access Form values in my Symfony executeAction backend

So one of my pages consists of a quiz form that has several questions of type multiple choice, the choices are specified as radio buttons. I have been scanning the Symfony documentation to find out how to access form field values inputted by the user. Thing is, this isn;t a doctrine or propel based form, and neither do I require the values to be stored in the database, hence executing a $form->save() makes little sense to me. But I do require access to specific values of my form in my backend once the user hits submit.
Most of Symfony documentation that i have run into doesn't necessarily explain how this can be done. I would assume it would be something to the effect of :
$request->getParameter( 'radio_choices_id selected value ').
Thanks to all who read this and Cheers to the ones who respond to it :)
Parijat
Hm, it is very simple if I understand your question right)
For widget:
$this->widgetSchema['name'] = new sfWidgetFormChoice(array('choices' => array('ch_1', 'ch_2')));
Ok,in action:
$this->form = new FaqContactForm();
if ($request->isMethod('post')) {
$this->form->bind($request->getParameter($this->form->getName()));
if ($this->form->isValid()) {
$your_val=$this->form->getValue('name');
//or
$your_val=$this->form['name']->getValue());
}
}
In backend in protected function processForm(sfWebRequest $request, sfForm $form)
you have
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
if ($form->isValid())
{
$notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';
try {
$product = $form->save();
} catch (Doctrine_Validator_Exception $e) {
$errorStack = $form->getObject()->getErrorStack();
$message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ? 's' : null) . " with validation errors: ";
foreach ($errorStack as $field => $errors) {
$message .= "$field (" . implode(", ", $errors) . "), ";
}
$message = trim($message, ', ');
$this->getUser()->setFlash('error', $message);
return sfView::SUCCESS;
}
Before $product = $form->save(); try
$your_val=$form->getValue('name');
//or
$your_val=$form['name']->getValue());