The code may not make much sense . This is just me just getting to know to code :)
I am making a chat where i am getting the chat messages from a mysql database and i want to output the data from the database in separate div`s using another function. I have som dificulties getting this to work!
Function to get the data from the database
function messages($user_id) {
$user_id = (int)$user_id;
$data = mysql_fetch_assoc(mysql_query("SELECT chat_content.chat_content_id, user.user_id, user.username, chat_content.chat_content, group_info.group_name FROM user, chat_content, group_info WHERE user.user_id = chat_content.user_id AND group_info.group_info_id = chat_content.group_info_id AND user.user_id = '$user_id'"));
return $data;
}
Function to output the data from the other function to a page
function messages_structure($user_id) {
messages($user_id);
$chat_id = $data[0]['chat_content_id'];
$output = array();
foreach($chat_id) {
$output[] = '<div id="chat_main_textbox"' . $chat_id . '</div>';
}
return '<div id="wrap">' .implode('', $output). '</div>';
}
Page output
$user_id = 56;
echo messages_structure($user_id);
messages() returns an array, you need to assign that return value to a variable with messages_structure() where you call it:
$data = messages($user_id);
If you don't, the $data variable that is created in messages() is only available with that function's scope.
I think your code should be
function messages($user_id) {
$user_id = (int)$user_id;
$data = mysql_fetch_assoc(mysql_query("SELECT chat_content.chat_content_id, user.user_id, user.username, chat_content.chat_content, group_info.group_name FROM user, chat_content, group_info WHERE user.user_id = chat_content.user_id AND group_info.group_info_id = chat_content.group_info_id AND user.user_id = '$user_id'"));
return $data;
}
Function to output the data from the other function to a page
function messages_structure($user_id) {
$data=messages($user_id);
$chat_id = $data[0]['chat_content_id'];
$output = array();
foreach($chat_id) {
$output[] = '<div id="chat_main_textbox"' . $chat_id . '</div>';
}
return '<div id="wrap">' .implode('', $output). '</div>';
}
Page output
$user_id = 56;
echo messages_structure($user_id);
Related
I am trying to fetch a data for each chkey with a related value as a group in codeigniter. When there are a 2 or more chkey then code is work properly.
But when there is a only 1 chkey then it shows value of that chkey but it is shows extra chkey:null,value:null in json.
Related json for chkey is 1 is as follows'
[{"unit_id":"8","CHGRAPHUpdatetime":{"time":"2018-03-15 00:00:00,2018-03-15 00:00:00,2018-03-15 00:00:00,2018-03-15 00:00:00"},"channelGraph":[{"chkey":"ch1","list":"2,-30,12,20"},{"chkey":"null","list":"null"}]}]
Expected is,
[{"unit_id":"8","CHGRAPHUpdatetime":{"time":"2018-03-15 00:00:00,2018-03-15 00:00:00,2018-03-15 00:00:00,2018-03-15 00:00:00"},"channelGraph":[{"chkey":"ch1","list":"2,-30,12,20"}]}]
Could you please help me to resolve this issue.
model code-
public function chGraph($unitid){
$this->db->select("unit_id");
$this->db->from("device_unit");
$this->db->where('unit_id', $unitid);
$query = $this->db->get();
$unit = $query->result_array();
$j = 0;
foreach($unit as $row) {
$unit[$j]['CHGRAPHUpdatetime'] = $this->UnitCHGRAPHUpdatetime($row['unit_id']);
$unit[$j++]['channelGraph'] = $this->UnitCHGRAPHDetails1($row['unit_id']);
}
return $unit;
}
public function UnitCHGRAPHDetails1($unit_id)
{
$this->db->select('distinct(chkey)','chvalue');
$this->db->from('channel_info');
$query = $this->db->get();
$channelgraphData = $query->result_array();
$m = 0;
foreach($channelgraphData as $row) {
$chvalueQuery = 'SELECT chkey, GROUP_CONCAT(chvalue)as list FROM channel_info where chkey= "'. $row['chkey'] .'" and unit_id='. $unit_id .'';
$response = $this->db->query($chvalueQuery)->row();
$channelgraphData[$m++] = $response;
}
return $channelgraphData;
}
controller code-
public function chGraph()
{
$unitid = $this->uri->segment('3');
$GraphDetails = $this->device_model->chGraph($unitid);
echo json_encode($GraphDetails);
}
You could add a GROUP BY at the end of the statement to eliminate the null results :
$chvalueQuery = 'SELECT chkey, GROUP_CONCAT(chvalue)as list FROM channel_info where chkey= "'. $row['chkey'] .'" and unit_id='. $unit_id .' GROUP BY chkey';
I have a community site, where people can put up pages, and the url generates from their name. e.g. My Business, becomes my-business. I want to create a way, that if there is another My Business, it will check and make the url my-business-2, my-business-3, etc.
function check_url($url) {
$qry = mysqli_query($this->con, "SELECT * FROM businesses WHERE url LIKE '$url%'");
if(mysqli_num_rows($qry)>0) {
$slugs = array();
while($row = mysqli_fetch_array($qry)) $slugs[] = $row['url'];
if(in_array($url, $slugs)) {
$max = 1;
while(in_array(($url . '-' . ++$max ), $slugs)) $url .= '-' . $max;
}
}
return $url;
}
This is my function, but this still wont work, as if there is a business called My Bus, it will make it my-bus-2, when it would have been unique at my-bus. I have experimented with other functions too, but this one is the closest I got. Can anyone tell me one that works perfectly?
So form my understanding, you are trying to avoid inserting/generating duplicate URL. Output will be something like this -
www.example.com/my-business
www.example.com/my-business-1
www.example.com/my-business-2
www.example.com/my-business-3
...
Try using this function -
function check_url($base_url, $new_url='', $num=0) {
if($new_url == '') {
$new_url = $base_url;
}
$qry = mysqli_query($this->con, "SELECT * FROM businesses WHERE url = '$new_url'");
if(mysqli_num_rows($qry) > 0) {
while($row = mysqli_fetch_array($qry)) {
$num++;
check_url($base_url, $base_url . '-' . $num, $num);
}
}
return $url;
}
Basically, this function will check the database until it finds a valid URL. Each time it will increment the number and recursively call the same function to generate new/valid URL.
Simple and basic!
#SpritsDracula has working code, but his function will query your database each time it executes. That being said, the recursion is a nice concept. Here is a piece of code that will save your the multiple database calls.
function check_url($url) {
$qry = mysqli_query($this->con, "SELECT * FROM businesses WHERE url LIKE '$url%'");
if(mysqli_num_rows($qry)>0) {
$baseUrl = $url;
$slugs = [];
while($row = mysqli_fetch_array($qry)) $slugs[] = $row['url'];
$max = 1;
while(in_array($url, $slugs)) {
$url = $baseUrl . "-" . $max++;
}
}
return $url;
}
I am trying to save what will amount to a small list of ids in user meta, but for some reason I am only able to save the most recent visit. Is there something obviously wrong with my approach?
function check_visit() {
$user = get_current_user_id();
$post_visits = get_user_meta($user, 'post_visits', true);
$visited = explode(",",$post_visits);
$id = (string)the_ID();
if($id && !in_array($id, $visited)) {
$visited[] = $id;
update_user_meta($user, 'post_visits', implode(",", $visited));
}
print_r(implode(",",$visited));
}
Actually
$visited[] = $id
is correct!
The issue is that you are using the_ID() function, this function print the ID, do not return any value.
The correct function should be get_the_ID()
Your code should look like this:
function aw_check_visit(){
$user = get_current_user_id();
$post_visits = get_user_meta($user, 'post_visits', true);
$visited = explode(",",$post_visits);
$id = (string)get_the_ID();
if($id && !in_array($id, $visited)) {
$visited[] = $id;
update_user_meta($user, 'post_visits', implode(",", $visited));
}
}
You need to be using array_push instead of adding it to the array like you have.
Change this
$visited[] = $id;
to
array_push($visited, $id);
I am a student who learns CodeIgniter, I have a school assignment about the database, I create a project, and run query in my model just like this.
class Send_model extends CI_Model{
function hello(){ $table = $this->db->query("SELECT * FROM (`tbl1`) LEFT JOIN `tbl2` ON `tbl2`.`id` = `tbl1`.`child_id` LEFT JOIN `tbl3` ON `tbl3`.`id` = `tbl1`.`child_id` ");
foreach($table->result() as $row){
$modbus[]= $row->modbus;
$data []= $row->data;
$alert[]= $row->alert;
};
$param0 = '&'.$modbus[0].'='.$data[0].':'.$alert[0];
$param1 = '&'.$modbus[1].'='.$data[1].':'.$alert[1];
$param2 = '&'.$modbus[2].'='.$data[2].':'.$alert[2];
$param3 = '&'.$modbus[3].'='.$data[3].':'.$alert[3];
$param4 = '&'.$modbus[4].'='.$data[4].':'.$alert[4];
$param5 = '&'.$modbus[5].'='.$data[5].':'.$alert[5];
$param6 = '&'.$modbus[6].'='.$data[6].':'.$alert[6];
$param7 = '&'.$modbus[7].'='.$data[7].':'.$alert[7];
$param8 = '&'.$modbus[8].'='.$data[8].':'.$alert[8];
$param9 = '&'.$modbus[9].'='.$data[9].':'.$alert[9];
$param10 = '&'.$modbus[10].'='.$data[10].':'.$alert[10];
$param11= '&'.$modbus[11].'='.$data[11].':'.$alert[11];
$param12 = '&'.$modbus[12].'='.$data[12].':'.$alert[12];
$param13 = '&'.$modbus[13].'='.$data[13].':'.$alert[13];
$param14 = '&'.$modbus[14].'='.$data[14].':'.$alert[14];
$param15 = '&'.$modbus[15].'='.$data[15].':'.$alert[15];
$param16 = '&'.$modbus[16].'='.$data[16].':'.$alert[16];
$param17 = '&'.$modbus[17].'='.$data[17].':'.$alert[17];
$param18 = '&'.$modbus[18].'='.$data[18].':'.$alert[18];
$sent = $param0.$param1.$param3.$param4.$param5.$param6.$param7.$param8.$param9.$param10.$param11.$param12.$param13.$param13.$param14.$param15.$param16.$param17;
return $sent;
}
my controller just like this
class Send extend CI_Controller{
function data ()
{
$this->load->model('send_model');
$send = $this->model->send_model->hello();
echo $sent;
}
}
i have a problem,if tbl1 add data then i have to write adding code to my script
can anyone help me to simplify this code?
It is because you're hard-coding to send only 19 rows of data by creating 19 $param variables. In a dynamic condition, you may have an empty record-set or hundreds of records. Modify the structure of your foreach loop to the following:
$param = array();
foreach($table->result() as $row){
$param[] = '&'.$row->modbus.'='.$row->data.':'.$row->alert;
}
return $param;
Hope it answers your question.
I am trying to call a model from my controller, which in turn generates data from a view, saves it as content and returns it to the controller, which then adds it to a template I have.
Whats happening is that $content = $this->load->view('left/profile', $c_data); is printing the data instead of saving it in variable $content
Here is my code:
Controller:
function index($page = '1-Welcome to')
{
if (!$this->tank_auth->is_logged_in()) {
//display non-login page
redirect('/auth/login/');
} else {
//user information
$user['user_id'] = $this->tank_auth->get_user_id();
$user['username'] = $this->tank_auth->get_username();
//general page data
$main['title'] = $this->page_model->make_title(ucfirst($page));
//template data
$template['head'] = $this->load->view('templates/head', $main, TRUE);
//get left content
$c_data['make_profile'] = $this->left_model->make_profile($user);
//combine into one variable
$data['template'] = $template;
$data['page'] = $page;
$data['user'] = $user;
$data['left'] = $c_data;
print_r($data);
$this->load->view('main_template', $data);
}
}
Focus on $c_data['make_profile'] = $this->left_model->make_profile($user);
Here is make_profile
public function make_profile($user)
{
$user_id = $user['user_id'];
$query = $this->db->query(" SELECT location FROM user_profiles AS up
INNER JOIN avatars AS a
ON up.avatar_id = a.id
WHERE user_id='$user_id'");
$c_data['avatar_loc'] = $query->row_array();
$content = $this->load->view('left/profile', $c_data);
$content .= "hello";
return $content;
}
And here is my profile view:
<div id="profile">
<div id='avatar'><img src="<?php echo $avatar_loc['location'] ?>" alt="avatar_user"/></div>
<div id="profile_pop"></div>
</div>
Any idea why it isn't working? Thanks
Return the data from the view as a string:
$this->load->view('left/profile', $c_data, TRUE);
Read here (bottom of the page).