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
Related
I'm having a problem with some PHP / MySQL code.
I need a view called gameview for a Star Wars game I'm writing.
If I created the view in MySQL then the code runs perfectly. However, I need this view to be dropped every time the game starts. So if I start without the view "gameview" present in the DB, the page cannot be displayed due to the view not existing. However, the moment I manually add the view into MySQL, it works. I can't see why.
Class code
<?php
class gameView
{
protected $Conn;
public function __construct($Conn)
{
$this->Conn = $Conn;
}
public function dropGameView()
{
$drop = "DROP VIEW if EXISTS gameview;";
$stmt = $this->Conn->prepare($drop);
$stmt->execute(array());
}
public function createGameView()
{
$view = "CREATE VIEW gameview AS SELECT id, name, image, quote FROM person;";
$stmt = $this->Conn->prepare($view);
$stmt->execute(array());
}
public function useGameView()
{
$query = "SELECT * from gameview";
$stmt = $this->Conn->prepare($query);
$stmt->execute(array());
$gameView = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $gameView;
}
}
?>
PHP code
<?php
$gameView = new gameView($Conn);
$finalCharacter = $gameView->useGameView();
$smarty->assign('game_view', $finalCharacter);
?>
Well.... stone the crows. I thought this would be too simple to work, but it did!
<?php
$gameView = new gameView($Conn);
$dropGameView = $gameView->dropGameView();
$smarty->assign('drop_gameview', $dropGameView);
$createGameView = $gameView->createGameView();
$smarty->assign('create_gameview', $createGameView);
$finalCharacter = $gameView->useGameView();
$smarty->assign('game_view', $finalCharacter);
?>
Now to crack on and use the view.
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.
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 want to list blogs with user membership on user home page as or whatever possible tag. yet I want every listed blog to launch a divert() function onClick. This function should assign a new $_SESSION[] variable, and then redirect the user to the blog.php (blog page). In the code below, divert() function launch once I call the home page.
include_once("inc/dbconnect.php");
session_start();
function divert($x) {
$_SESSION['blnm'] = $x;
header("Location: blog.php");
}
function listing() {
global $conn;
$member = $_SESSION['u'];
$outquery = "SELECT * FROM blogmembership WHERE blogmember='$member'";
$result = mysqli_query($conn, $outquery);
$no = mysqli_num_rows($result);
while ($row = mysqli_fetch_array($result)) {
$list = $row['blogname'];
echo "<li><a onClick='".divert($list)."' id='".$list."'>".$list."</a> </li><br />";
//How to launch the "divert()" function onClick only?
}
}
I have a resource:
Route::resource('artists', 'ArtistsController');
For a particular url (domain.com/artists/{$id} or domain.com/artists/{$url_tag}), I can look at the individual page for a resource in the table artists. It is controlled by this function:
public function show($id)
{
if(!is_numeric($id)) {
$results = DB::select('select * from artists where url_tag = ?', array($id));
if(isset($results[0]->id) && !empty($results[0]->id)) {
$id = $results[0]->id;
}
}
else {
$artist = Artist::find($id);
}
$artist = Artist::find($id);
return View::make('artists.show', compact('artist'))
->with('fans', Fan::all())
->with('friendlikes', Fanartist::friend_likes())
->with('fan_likes', Fanartist::fan_likes());
}
What I would like to do is have all urls that are visited where the {$id} or the {$url_tag} don't exist int he table, to be rerouted to another page. For instance, if I typed domain.com/artists/jujubeee, and jujubee doesn't exist in the table in the $url_tag column, I want it rerouted to another page.
Any ideas on how to do this?
Thank you.
In your show method you may use something like this:
public function show($id)
{
$artist = Artist::find($id);
if($artist) {
return View::make('artists.show', compact('artist'))->with(...)
}
else {
return View::make('errors.notfound')->withID($id);
}
}
In your views folder create a folder named errors (if not present) and in this folder create a view named notfound.blade.php and in this view file you'll get the $id so you may show something useful with/without the id.
Alternatively, you may register a global NotFoundHttpException exception handler in your app/start/global.php file like this:
App::error(function(Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e) {
// Use $e->getMessage() to get the message from the object
return View::make('errors.notfound')->with('exception', $e);
});
To redirect to another page have a look at the redirect methods available on the responses page of the Laravel docs.
This is how I would go about doing it and note that you can also simplify your database queries using Eloquent:
public function show($id)
{
if( ! is_numeric($id)) {
// Select only the first result.
$artist = Arist::where('url_tag', $id)->first();
}
else {
// Select by primary key
$artist = Artist::find($id);
}
// If no artist was found
if( ! $artist) {
// Redirect to a different page.
return Redirect::to('path/to/user/not/found');
}
return View::make('artists.show', compact('artist'))
->with('fans', Fan::all())
->with('friendlikes', Fanartist::friend_likes())
->with('fan_likes', Fanartist::fan_likes());
}