function create()
{
$data['headline'] =" ";
$this->load->module('site_security');
$this->site_security->_make_sure_is_admin();
$update_id = $this->uri->segment(3);
if(!is_numeric($update_id))
{
$data['headline'] = "Add New Product";
//$data['headline'] = print_r($update_id)."if";
}
else
{
$data['headine'] = "Update Product Details";
//$data['headline'] = print_r($update_id)."else";
}
$data['view_module'] = "store_products"; //passing module name
$data['view_file'] = "create"; //passing method name
$this->load->module('templates'); // loads the template module
$this->templates->admin($data); //calls the template controller method admin which loads the admin view
}
I am passing the headline text through to change heading in a view depending on the content of the third 3rd segment in the url. The first if statement sets the heading perfectly but it wont go into the else statement and was giving me errors because $headline was undefined in the view. Oddly enough if i use the commented print_r statements everything works perfect it will display it in the view.
Im thinking it has to be the line $data['headine'] = "Update Product Details"; in the else but i dont know why.
In your else statement,
else
{
$data['headine'] = "Update Product Details";
}
You have a typo here. change headine to headline.
Related
I am building a website with CodeIgniter, and I ran into a problem:
When I load the view of the song page I also want to update in database the view count by one, but it is updating by two. I don't understand why, this happens, because when I try the same method when the download button is pressed to update the download count, that one works fine.
Here is the code:
function view($slug) {
// Get the song from database by slug
$data['song'] = $this->mdl_song->get_where_row("slug", $slug);
// if thre is a song, continue
if ($data['song']) {
// Set data to send to the view file
$data['module'] = "song";
$data['view_file'] = "single";
$data['page_title'] = "Download {$data['song']->name}";
// echo the template that display the view
echo Modules::run('template/two_col', $data);
// Update the song hits
$this->mdl_song->_update($data['song']->id, array('hits' => $data['song']->hits + 1));
} else {
show_404();
}
}
function _update($id, $data) {
$table = $this->get_table();
$this->db->set($data);
$this->db->where('id', $id);
$this->db->update($table);
}
I am new to php/mvc and experimenting with a small website.
I would like to change to state of a button depending on a select query. It's a simple 'favourite' toggle button. The query will look to the 'favourite' table of my db for the 'isFav' field (0 or 1).
I would like the button colour to be green (btn-success) if the query finds a result (1), and the colour to be default (btn-default) if not (0).
Should be simply but I can't seem to get it. I think I am getting confused regarding passing of the variables between my view and model.
My books_model.php code is as follows
class BooksModel
{
public function isFav()
{
$book_id = $_GET['id']; // from url
$user_id=$_SESSION['user_id'];
$sql = "SELECT isFav FROM favourite WHERE book_id = :book_id AND user_id = :user_id AND isFav = 1";
$query = $this->db->prepare($sql);
$query->bindParam(':book_id', $book_id);
$query->bindParam(':user_id', $user_id);
$query->execute();
if ($query->rowCount() == 1) {
$css = 'btn-success';
echo $css; //for testing
} else {
$css = 'btn-default';
echo $css; // for testing
}
}
}
My books.php controller is as follows;
class Books extends Controller
{
function itemView()
{
$itemView_model = $this->loadModel('Books');
$this->view->books = $itemView_model->itemView();
$this->view->render('books/itemView');
$itemView_model->isFav();
}
}
My itemView.php html button code is as follows;
echo '<button class="btn '.$css.'"></button>';
The only thing printed on the page is a btn-success or btn-default (top left). This toggles whenever I click the button. Therefore I assume my query is working.
Apologies for the code I am a newbie and I do appreciate any help offered, even a point in the right direction.
Try this:
public function isFav() {
(...)
if ($query->rowCount() == 1) {
$css = 'btn-success';
echo $css; //for testing
} else {
$css = 'btn-default';
echo $css; // for testing
}
return $css;
}
And in your View:
echo "<button class=\"btn ".$itemView_model->isFav()."\"></button>";
The reason: Your Variable $css in your isFav() method is not known in your view (the scope of the variable is the isFav() method).
This line:
$itemView_model->isFav();
Requests the return value of this function, so, clearly, the value bust be returned in order to be available.
The reason you see the seemingly correct answer printed is that the echo call is in the same scope as the $css variable.
I'm working in PHP, model view controller.
I run a action that is called getallmypictures where the result is shown on a view. When I click the result picture I run another action that get's all my related products to this picture, and that result it shown on another view file.
How can I get the second action result to be shown in a overlay on the same page as my pictures views?
This is my first action getallmypictures() when its execute:
public function GetAllPicturesAction(){
//if (isset($_GET['showAll'])) {
//if (isset($_SESSION['status']) == "customers_inloggad") {
$db = new PDO("mysql:host=localhost;dbname=fashionable", "root", "");
$stm1 = $db->prepare("MYSQL");
if ($stm1->execute()) {
$res = $stm1->fetchAll();
require_once './views/pictureInspo.php';
}
else {
header("location:../start/index?msgWrong=empty");
}
}
and the other action getthispicture() is required to this PHP file to show the result:
public function GetThisPictureAction(){
if(isset($_GET['getPictureValue'])){
// if (isset($_SESSION['status']) == "customers_inloggad") {
$db = new PDO("mysql:host=localhost;dbname=fashionable", "root","");
$stm2 = $db->prepare("MYSQL");
$stm=$db->prepare("MYSQL");
$stm->bindParam(":picture_inspo_id", $_GET['getPictureValue']);
$stm2->bindParam(":picture_inspo_id",$_GET['getPictureValue']);
if ($stm2->execute()) {
$result = $stm2->fetchAll();
$stm->execute();
$result1 = $stm->fetchAll();
require_once './views/pictureInspo_overlay.php';
} }//}
else {
header("location:../start/index?msgWrong=empty");
}
}
instead of showing to actions on 2 views I would like to have a second view like an overlay but still keep them in different actions.
The routing im using is /controller/action
I am trying to submit a EDIT form which edits Users Academics Details,
These Details have unique id in DB and my Code in Short Looks like below :
class edit extends ci_controller
{
function user_academics($id = NULL)
{
if(isset($id) == FALSE) //if link is ./edit/user_academics
{
$id = NULL;
$link = site_url('profile');
show_error("Invalid Page Request! <a href='$link' Go to Profile </a>");
}
$user_id = $this->session->userdata('user_id');
$data['fill'] = $this->edit_model->get_user_academics($id);
if($user_id != $data['fill']['user_id']) // check if logged in user is accessing his record or others
{
$link = site_url('profile');
show_error("This is an Invalid Request ! <a href='$link'>Go to Profile </a>");
}
else // actual work starts here
{
$this->session->set_flashdata('ua_id',$id); // update_academics will get this data
$this->load->view('edit/edit_3_view',$data);
}
}
function update_academics()
{
$ua_id = $this->session->flashdata('ua_id'); // flash data used here .
if( !$ua_id )
{
show_error('Sorry, This request is not valid!');
}
$academics = array(
// All post values
);
$this->edit_model->update_user_academics($academics,$ua_id);
//print_r($academics);
redirect('profile');
}
}
Now the problem is
- If I open two different records to edit, then It will set only one Session Flash value.
- And No matter what I edit , the existing values of the last flash value gets updated.
Please Suggest me another way or Correct me if I am wrong in above code . Thanks
save that flashdata in array, like:
$myArr = array('value 1', 'value 1');
//set it
$this->session->set_flashdata('some_name', $myArr);
And in view:
$dataArrs = $this->session->flashdata('some_name');
//loop thru $dataArrs to show the flashdata
Flash data is simply like variable which is available only in next request, you can bypass this behavior by using two different keys with record id in it, so that when you use flash data for showing message you can access key with particular record id.
Okay, so I have this snippet of code in a controller. However, it's all DB driven and should really be in model - I get that. However, as you can see in the IF statement, I need to pass along $data to my view. Based on the outcome. I tried pasting this chuck of coding in a method in my model (calling the model method via controller), however the $data[update_prompt] string is not getting called by the view...
How would I translate this code into a model - sending the $data values back to my controller to embed in my view?
// show appropriate upgrade message if user has free account
$id = $this->session->userdata('user_id');
$this->db->select('subscription'); // select the subscription column
$this->db->where('id', $id); //find id in table that matches session id
$query = $this->db->get("subscriptions"); // connect to this database
$subscribe = $query->result_array(); //returns the result of the above
if($subscribe[0]['subscription'] == 'freebie') // if subscription column equals 'freebie' in the $subscribe array, do this:
{
$data['update_prompt'] = $this -> load -> view('shared/upgrade_subscription', '', TRUE); // adds view within view, $update_prompt
}
else
{
$data['update_prompt'] = '';
}
You would add a function in your model, like so:
public function myModelFunction($id) {
//we return row as we are looking up by primary key and are guaranteed only one row
return $this->db->select('subscription')
->where('id', $id)
->get('subscriptions')
->row();
}
Then, in your controller:
public function myControllerFunction() {
$subscribe = $this->my_model->myModelFunction($this->session->userdata('id'));
if($subscribe->subscription == 'freebie') // if subscription column equals 'freebie' in the $subscribe array, do this:
{
$data['update_prompt'] = $this -> load -> view('shared/upgrade_subscription', '', TRUE); // adds view within view, $update_prompt
}
else
{
$data['update_prompt'] = '';
}
}