php with framework of codeigniter - php

function invitation_result()
{
$this->load->database();
$user_id = $_GET['user_id'];
$qry = mysql_query("select * from sent_invitations where user_id='$user_id'");
if (mysql_num_rows($qry) > 0)
{
while ($row = mysql_fetch_array($qry))
{
$mobile_number = $row['mobile_number'];
$qry1 = mysql_query("select mobile_number from users where mobile_number = '$mobile_number'");
$row1 = mysql_fetch_array($qry1);
$mobile_number = $row1['mobile_number'];
$users[] = array('mobile_number' => $mobile_number );
return $users;
}
}
}
The above is my coding.
In sent_invitation table there are 3 rows with user_id=11.
from there i m trying to get mobile_number of all three rows based on user_id.
the mobile_number of all 3 rows is different.
after geting mobile_number from sent_notification table.
i m comparing the mobile_number with mobile_number of users table.
i m seeing that 3 of them mobile_numbers are there in users table.
i m trying to show all of them.
bt problem is that my code id showing only one row and that is of last mobile number from 3

Since you are using Codeigniter You should follow the MVC pattern :
So the code in the controller should be :
public function invitation_result(){
$userId = 0;
if(($_GET['user_id']) && is_numeric($_GET['user_id']) && $_GET['user_id'] > 0){
$userId = $_GET['user_id'];
}
$data = $this->User_model->getDataFromID($userId);
return $data;
}
And the code for Model should be :
public function getDataFromID($userId){
$arrReturn = array();
if(($userId) && is_numeric($userId) && $userId > 0){
$this->db->select('*');//You can put the required fields here like : name,mobile_number...
$this->db->from('sent_invitations');
$this->db->where("user_id",$user_id);
$query = $this->db->get();
$result = $query->result_array();
if(!empty($result)){
foreach($result as $key=>$value){
$this->db->select('*');
$this->db->from('mobile_number');
$this->db->where("mobile_number",$value['mobile_number']);
$querySub = $this->db->get();
$resultSub = $querySub->result_array();
if(!empty($resultSub)){
array_push($arrReturn,$resultSub);
}
}
return $arrReturn;
}
}else{
return $arrReturn;
}
}
By the time I have written the Answer you would have solved the error ,but this answer will help future Users.

You should use while loop where you are getting mobile_number in inner loop.
function invitation_result()
{
$this->load->database();
$user_id = $_GET['user_id'];
$qry = $this->db->query("select * from sent_invitations where user_id='$user_id'");
if ($qry->num_rows() > 0)
{
while ($qry->result_array() as $row)
{
$mobile_number = $row['mobile_number'];
$qry1 = $this->db->query("select mobile_number from users where mobile_number = '$mobile_number'");
if($qry1->num_rows() > 0){
while($qry1->result_array() as $row1){
$mobile_number = $row1['mobile_number'];
$users[] = array('mobile_number' => $mobile_number );
}
return $users;
}
}
}
}
You should use codeigniter database query instead of mysql custom query. because mysql is deprecated. You can use below code:
function invitation_result()
{
$this->load->database();
$user_id = $_GET['user_id'];
$qry = mysqli_query("select * from sent_invitations where user_id='$user_id'");
if (mysql_num_rows($qry) > 0)
{
while ($row = mysql_fetch_array($qry))
{
$mobile_number = $row['mobile_number'];
$qry1 = mysql_query("select mobile_number from users where mobile_number = '$mobile_number'");
$row1 = mysql_fetch_array($qry1);
$mobile_number = $row1['mobile_number'];
$users[] = array('mobile_number' => $mobile_number );
return $users;
}
}
}

first, can you simplified the query something like this ?
$sql = "select a.*, b.mobile_number as user_mobile_number
from sent_invitations a
join users b on a.user_id = b.user_id";
or
$sql = "select a.*, b.mobile_number as user_mobile_number
from sent_invitations a
join users b on a.mobile_number = b.mobile_number";
it's depend on how this two tables can be related.
Now you can see if the sent_invitations.mobile_number also found in users.mobile_number just in one query. If this returns multiple same rows, then put distinct keyword after SELECT
second, back to your code, i think you should move return $users out form while block
while ($row = mysql_fetch_array($qry)) {
// your code
}
return $users;
good luck;

function invitation_result()
{
$this->load->database();
$user_id = $_GET['user_id'];
$qry = $this->db->query("select * from sent_invitations where user_id='$user_id'");
$results = $qry->result();
foreach ($results as $result)
{
$mobile_number = $result->mobile_number;
$qry1 = $this->db->query("select mobile_number from users where mobile_number = '$mobile_number'");
$results1 = $qry1->result();
foreach ($results1 as $result1)
{
$row1 = mysql_fetch_array($qry1);
$mobile_number = $result1->mobile_number;
$users = array('mobile_number' => $mobile_number );
return $users;
}
}
}

Related

join 2 table in one function

i want to join the data of 2 tables in 1 table and display the data of 2 tables. I already have function that load the one table. can someone modify this function code to get the 2 tables ? im just a beginner in php.
Model_users.php
public function getUserGroup($userId = null)
{
if($userId) {
$sql = "SELECT * FROM user_group WHERE user_id = ?";
$query = $this->db->query($sql, array($userId));
$result = $query->row_array();
$group_id = $result['group_id'];
$g_sql = "SELECT * FROM groups WHERE id = ?";
$g_query = $this->db->query($g_sql, array($group_id));
$q_result = $g_query->row_array();
return $q_result;
}
}
Controller User.php
public function index()
{
if(!in_array('viewUser', $this->permission)) {
redirect('dashboard', 'refresh');
}
$user_data = $this->model_users->getUserData();
$result = array();
foreach ($user_data as $k => $v) {
$result[$k]['user_info'] = $v;
$group = $this->model_users->getUserGroup($v['id']);
$result[$k]['user_group'] = $group;
}
$this->data['user_data'] = $result;
$this->render_template('users/index', $this->data);
}
Here you go
$this->db->select("ug.*,g.*");
$this->db->from("user_group ug");
$this->db->join('groups g', 'ug.user_id = g.group_id');
$this->db->where("ug.user_id",1);
$query = $this->db->get();
if ($query->num_rows() > 0) {
$result = $query->result();
} else {
$result = null;
}
return $result;

After fetching a value from DB then assign it to a variable, then i can't use it to the next query

This is my model:
public function GetStudentData()
{
$sID = $this->input->get('id');
$class_id = $this->db->query("SELECT student.class_id FROM student WHERE student.sID = '$sID'");
$query = $this->db->query("SELECT * FROM student WHERE(sID='$id'AND class_id = '$class_id')");
if($query->num_rows() > 0)
{
return $query->row();
}else{
return false;
}
}
How can I use $class_id in the next query?
you could do it in one query... (from your example [SIC])
public function GetCSData() {
$id = $this->input->get('id');
$query = $this->db->query("SELECT student.*
FROM student
WHERE student.sID = '$id'
AND student.class_id = (SELECT class_id
FROM student
WHERE student.sID = '$id')");
if($query->num_rows() > 0){
return $query->row();
}else{
return false;
}
}//..GetCSData end
looking at it though, that will give the same result as
SELECT *
FROM student
WHERE sID = '$id'
maybe I am missing a point?

passing array and checking the values

While creating a group with user ids, I am checking if the entered id exists in database. If it exists, group should be created, if not it should pop out an error.
I am passing array with user ids to a function user_check, but it is not getting checked. Here is my code:
<?php
function user_check($user_id)
{
$u1 = mysqli_query($con, "SELECT uid from users where uid<>'" . $_SESSION['user_id'] . "'");
while ($row = mysqli_fetch_array($u1)) {
$users = $row['uid'];
echo "users" . $users;
$users1 = sort($users);
$users2 = sort($user_id);
printf($users1);
if ($user1 == $user2) {
echo "same";
} else {
echo "not same";
}
}
}
Given a list of user_ids (that you have validated/filtered already) you could write a function something like the following (untested):
function all_users_exist(array $user_ids, Mysqli $con)
{
$result_uids = [];
$ids = implode(',', $user_ids);
$sql = "SELECT uid from users WHERE uid IN ($ids)";
$result = $con->query($sql);
if($result)
while ($row = $result->fetch_array(MYSQLI_ASSOC))
$result_uids[] = $row['uid'];
sort($user_ids);
sort($result_uids);
return $user_ids == $result_uids;
}

push data in an array using codeigniter php

I have user_sport_tbl,sport_tbl tables. in user_sport_tbl i have sport_id but not have sport name.Sport name is in sport_tbl.
i have a function of getUserSport() of Model.
my query :-
$query = $this->db->get_where('user_sport_tbl',array('username' => $username));
i want sport_name for every sport_id and want to merge sport_name in array.
my code :-
public function getUserSport()
{
$username = $this->session->userdata('username');
$query = $this->db->get_where('user_sport_tbl',array('username' => $username));
if($query->num_rows > 0)
{
foreach($query->result() as $item)
{
$query1 = $this->db->query("select sport_name from sport_tbl where sport_id=$item->sport_name order by sport_id limit 1");
$sql1 = $query1->row();
$data[] = array_push($item, $sql1->sport_name);
}
return $data;
}
}
my present return data:-
[user_match_id] => 7 [sport_id] => 2 [match_id] => 3 [username] => admin
i want to add [sport_name] => cricket.
I want to return an array with sportname
Try this coding
public function getUserSport()
{
$data = array();
$username = $this->session->userdata('username');
$query = $this->db->get_where('user_sport_tbl',array('username' => $username));
if($query->num_rows > 0)
{
foreach($query->result() as $key=>$item)
{
$query1 = $this->db->query("select sport_name from sport_tbl where sport_id=$item->sport_name order by sport_id limit 1");
$sql1 = $query1->row();
$item[$key]['sport_name'] = $sql1->sport_name;
$data[] = $item;
}
return $data;
}
}
Try This
public function getUserSport()
{
$username = $this->session->userdata('username');
$query = $this->db->get_where('user_sport_tbl',array('username' => $username));
if(empty($query))
{
foreach($query->result_array() as $item)
{
$sportsId = $item[0]['sport_name'];
$query1 = $this->db->query("SELECT sport_name FROM sport_tbl WHERE sport_id = $sportsId ORDER BY sport_id LIMIT 1");
$result = $query1->result_array();
$data[] = array_push($item, $result[0]['sport_name']);
}
return $data;
}
}
Or can do with join query too (Not tested. If any error fix it.
or post the message her)
public function getUserSport()
{
$username = $this->session->userdata('username');
$query = $this->db->query(
"SELECT user_sport_tbl.sport_name, sport_tbl.sport_name
FROM user_sport_tbl
INNER JOIN sport_tbl
ON user_sport_tbl.username = sport_tbl.sport_name
WHERE user_sport_tbl.username = '$username' ORDER BY sport_tbl.sport_id LIMIT 1");
$result = $query->result_array();
return $data;
}

Search results with pagination code won't return results

I'm having a hard time getting this search results with pagination code to work. It does successfully grab the search keyword entered in the html form on another page and brings it into this search.php page. if I echo $search I see the keyword on the page. But I get no results even though I should for the query. Can anyone see what might be going on?
require "PDO_Pagination.php";
if(isset($_REQUEST["search_text"]) && $_REQUEST["search_text"] != "")
{
$search = htmlspecialchars($_REQUEST["search_text"]);
$pagination->param = "&search=$search";
echo $search;
$pagination->rowCount("SELECT * FROM stories WHERE stories.genre = $search");
$pagination->config(3, 5);
$sql = "SELECT * FROM stories WHERE stories.genre = $search ORDER BY SID ASC LIMIT $pagination->start_row, $pagination->max_rows";
$query = $connection->prepare($sql);
$query->execute();
$model = array();
while($rows = $query->fetch())
{
$model[] = $rows;
}
}
else
{
$pagination->rowCount("SELECT * FROM stories");
$pagination->config(3, 5);
$sql = "SELECT * FROM stories ORDER BY SID ASC LIMIT $pagination->start_row, $pagination->max_rows";
$query = $connection->prepare($sql);
$query->execute();
$model = array();
while($rows = $query->fetch())
{
$model[] = $rows;
}
}
$query = "SELECT * FROM stories";
if(isset($_REQUEST["search_text"]) && $_REQUEST["search_text"] != "")
{
$search = htmlspecialchars($_REQUEST["search_text"]);
$pagination->param = "&search=$search";
$query .= " WHERE genre LIKE '%$search%'";
}
// No need for else statement.
$pagination->rowCount($query);
$pagination->config(3, 5);
$query .= " ORDER BY SID ASC LIMIT {$pagination->start_row}, {$pagination->max_rows}";
$stmt = $connection->prepare($query);
$stmt->execute();
$model = $stmt->fetchAll();
var_dump($model);
In your query do:
WHERE stories.genre LIKE '%string%');
instead of:
WHERE stories.genre = 'string');
Because the equals will want to literally equal the field.

Categories