how to get image location from database using ID - php

I am working on my website using wordpress to view and change cover for user by clicking on photo and then redirect him for example to process.php?pid=12
I am using
switch($GetPicId)
{
So I can't add more pic I but now I have script to upload pic using database, and I wanna to get id and pic location from by using database
First this is the upload script
and this is switch code
$GetPicId = $_GET["pid"]; // Picture ID from Index page
$PicLocation ='';
/*
Users do not need to know original location of image.
I think it's better to get image location from database using ID.
for demo here i'am using PHP switch.
*/
switch($GetPicId)
{
case 1:
$PicLocation = 'cover_pics/cover1.jpg';
break;
case 2:
$PicLocation = 'cover_pics/cover2.jpg';
break;
case 3:
$PicLocation = 'cover_pics/cover3.jpg';
break;
case 4:
$PicLocation = 'cover_pics/cover4.jpg';
break;
case 5:
$PicLocation = 'cover_pics/cover5.jpg';
break;
case 6:
$PicLocation = 'cover_pics/cover6.jpg';
break;
case 7:
$PicLocation = 'cover_pics/cover7.jpg';
break;
case 8:
$PicLocation = 'cover_pics/cover8.jpg';
break;
case 9:
$PicLocation = 'cover_pics/cover9.jpg';
break;
case 10:
$PicLocation = 'cover_pics/cover10.jpg';
break;
case 11:
$PicLocation = 'cover_pics/cover11.jpg';
break;
default:
header('Location: ' . $homeurl);
break;
}

Just Try
$GetPicId = $_GET["pid"]; // Picture ID from Index page
$sql=mysql_query("SELECT piclocation FROM tableName WHERE pid=$GetPicId") or die(mysql_error());
$res=mysql_fetch_array($sql);
$PicLocation = $res['piclocation '];

Use this to get attachment url ..Your cover images are uploaded in media right??
Then try this
<?php wp_get_attachment_url( $id ); ?>
it will become something like this
$GetPicId = $_GET["pid"]; // Picture ID from Index page
$PicLocation =wp_get_attachment_url( $GetPicId);

Related

PHP Switch with simple strings not working

$url = $_SERVER['REQUEST_URI'];
$url = basename($url).PHP_EOL;
switch ($url) {
case 'digital-marketing-and-seo':
$new_url = "Digital Marketing & SEO";
break;
case 'websites-digital-destinations':
$new_url = "Websites & Digital Destinations";
break;
case 'brand-identity-evolution':
$new_url = "Brand Identity & Evolution";
break;
case 'strategy-consulting':
$new_url = "Strategy & Consulting";
break;
case 'government-universities':
$new_url = "Government & Universities";
break;
case 'hospitality-travel':
$new_url = "Hospitality & Travel";
break;
case 'architecture-engineering':
$new_url = "Architecture & Engineering";
break;
case 'wine-spirits':
$new_url = "Wine & Spirits";
break;
default:
$new_url = '';
echo 'default derp';
}
So I have this block of code and it will not work for me. The url can say exactly what is in the case and it will default. I'm trying to get url structures to build out a shortcode and I need it to match what is entered.
I determined that if I enter just add a variable for a string for $url = 'brand-identity-evolution'; The switch works. I made sure it's a string with is_string function and can't figure out why this isn't working.
You added a PHP_EOL at the end of the URL, so all of your cases will fail because you're not checking for that. Remove your PHP_EOL and you'll be fine.

Changing logo based on incoming domain using php

I am trying to change the logo based on what domain the visitor is using to view the website. I found some code in another topic on this site, but i was unable to make it work, so i was wondering if anyone of you can see what i'm doing wrong.
This is my code:
<?php
function logoSwap(){
switch($_SERVER['HTTP_HOST']) {
case 'liceng.dk':
$logo = "http://www.liceng.dk/images/liceng-logo.png";
break;
case 'www.liceng.dk':
$logo = "http://www.liceng.dk/images/liceng-logo.png";
break;
case 'licenergy.co.uk':
$logo = "http://www.liceng.dk/images/licenergy-logo.jpg";
break;
case 'www.licenergy.co.uk':
$logo = "http://www.liceng.dk/images/licenergy-logo.jpg";
break;
case 'http://licenergy.co.uk/':
$logo = "http://www.liceng.dk/images/licenergy-logo.jpg";
break;
default:
$logo = "http://www.liceng.dk/images/liceng-logo.png";
break;
};
return $logo;
}
?>
<?php
if(!empty($image)) {
echo '<h1>LIC Engineering A/S</h1>';
}
else {
echo '<img src="'.logoSwap().'" alt="LICeng logo">';
}
?>
Shouldn't it work? You can check the site here: www.liceng.dk

Steam API get current user status

I'm trying to use the steam web API to show the current status of ONE steam user if he is (online,away,offline,looking to trade) etc and have setup a PHP switch as follows:
<?
$status = $steamprofile['personastate'];
switch($status) {
case 0:
echo "offline!";
break;
case 1:
echo "online!";
break;
case 2:
echo "Busy.";
break;
case 3:
echo "Away.";
break;
case 4:
echo "Snooze.";
break;
case 5:
echo "Looking to trade.";
break;
case 6:
echo "Looking to play.";
break;
default:
echo "Unknown status";
}
echo $status;
?>
It works great but only shows the user status when logged unto the site and not simply when the user is online/away/offline etc on the steam platform.
I'm wondering if anyone know how to fix so it displays the current user status on steam, not just if he/she is logged in on the website.

How to get user input for switch statement in php?

As questioned above I'm trying to get user input in my PHP script to do further tasks. Is there any method in PHP to get user input like 'scanf()' used in C
<?php
echo "Swapping Numbers </br> Please select the method: </br>";
echo "1. Using 3rd variable </br> 2. Using add/sub </br> 3. Using mul/div";
//read user input $choice
switch($choice)
{
case 1:
break;
case 2:
break;
case 3:
break;
default:
echo "</br>You entered wrong choice";
}
?>
the STDIN constant is pre-defined and available for you to use immediately.
You can get user input using fgets and fscanf
<?php
$line = trim(fgets(STDIN)); // reads one line from STDIN
fscanf(STDIN, "%d\n", $number); // reads number from STDIN
For example
<?php
echo "Enter a number: ";
fscanf(STDIN, "%d\n", $number);
switch ($number) {
case 1: echo "one\n"; break;
case 2: echo "two\n"; break;
case 3: echo "three\n"; break;
default: echo "I can't count that high\n";
}
Check out the I/O docs for more detailed information
You should use readline() :
var_dump(readline('Enter number: '));
You can handle user input from form element or from url.
in your case you can use like this.
$choice = $_GET['choise'];
User will type like this
http://localhost/your_file_name.php?choise=option
than you should use your switch
switch($choice)
{
case 1:
break;
case 2:
break;
case 3:
break;
default:
echo "</br>You entered wrong choice";
}
?>

Uploaded and resized photo in Codeigniter is not available in view until I refresh .. is my view loading "too fast"?

I'm having problems with a photo upload system in Codeigniter.
I'm uploading and resizing a photo during registration in my controller, and once that goes through, I load an "upload successful" view where I want to display the photo again.
The problem is,
I step into a "fail" loop that actually seems to not be failing
and
the first time I load my view, the photo isn't displaying (although it has been uploaded, resized, and the path is in my DB). But if I refresh the page, it does display correctly.
Here is the controller .. check the comments close to the bottom, because the top part is basically just the resizing logic and (although it is kind of ugly right now) it does work
public function do_upload()
{
$config['upload_path'] = './upload_pic/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1200';
$config['max_height'] = '768';
$_maxResizedWidth = '242';
$this->load->library('upload', $config);
if ( $this->upload->do_upload() )
{
$this->data['upload_data'] = $this->upload->data();
$this->data['title'] = "Upload Successful";
switch($this->data['upload_data']['file_type']){
case "image/jpg":
$_file_suffix = ".jpg";
break;
case "image/jpeg":
$_file_suffix = ".jpg";
break;
case "image/png":
$_file_suffix = ".png";
break;
case "image/gif":
$_file_suffix = ".gif";
break;
case "image/pjpeg":
$_file_suffix = ".jpg";
break;
}
$_fileName = $this->data['upload_data']['file_name'];
$_oldUploadPath = $config['upload_path'] . $_fileName;
$_random_key = strtotime(date('Y-m-d H:i:s'));
$_newUploadPath = $config['upload_path'] . 'resize_' . $_random_key . $_file_suffix;
rename( $_oldUploadPath, $_newUploadPath );
$_oldSize = getimagesize($_newUploadPath);
$_oldWidth = $_oldSize[0];
if($_oldWidth > $_maxResizedWidth){
$_scale = $_maxResizedWidth/$_oldWidth;
$_oldHeight = $_oldSize[1];
$_imageType = image_type_to_mime_type($_oldSize[2]);
$_newWidth = $_oldWidth * $_scale;
$_newHeight = $_oldHeight * $_scale;
$_resizedImage = imagecreatetruecolor($_newWidth, $_newHeight);
switch($_imageType) {
case "image/gif":
$_source=imagecreatefromgif($_newUploadPath);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
$_source=imagecreatefromjpeg($_newUploadPath);
break;
case "image/png":
case "image/x-png":
$_source=imagecreatefrompng($_newUploadPath);
break;
}
imagecopyresampled($_resizedImage, $_source, 0, 0, 0, 0, $_newWidth, $_newHeight, $_oldWidth, $_oldHeight);
switch($_imageType) {
case "image/gif":
imagegif($_resizedImage,$_newUploadPath);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
imagejpeg($_resizedImage,$_newUploadPath,90);
break;
case "image/png":
case "image/x-png":
imagepng($_resizedImage,$_newUploadPath);
break;
}
}
$_newUploadPathWithoutDot = substr($_newUploadPath,1);
$this->load->model('Member_model');
$ID = $this->the_user->id;
/// ****************************************************************************
/// I step into this next if loop even though the upload_photo method does work.
/// In my view I get the "there was a problem" .. but the DB has been correctly updated
if(!$this->Member_model->upload_photo( $ID, $_newUploadPathWithoutDot));
{
$uploadData = $this->upload->data();
$this->data['message'] = "There was a problem entering your photo in the database ==";
};
/// And this is the view that's called
$this->load->view('upload_big_success_view', $this->data);
}
else
{
$this->data['message'] = "there was a problem " . $this->upload->display_errors() . dirname(__FILE__);
$this->data['title'] = "Upload Unsuccessful";
$this->load->view('profile_photo_view', $this->data);
}
}
(I know there is a lot of messy in there, and I'll give it a refactor).
It almost seems like the image is "not ready" when the view first loads, even though everything has worked - the upload, the resize, the filepath is entered in the DB... nothing has thrown any errors until it gets to that last loop and it seems to think "->upload_photo()" has failed when it hasn't. And then when I call the view the first time my ..
<img src"<?php echo $the_user->large_image_location; ?>" >
.. produces no output. But after a simple refresh, it does. Although the "there was a problem message" is still displaying in the view.
BTW - here is the model. Nothing fancy.
public function upload_photo($ID, $_newUploadPath)
{
$sql = "UPDATE users SET large_image_location=? WHERE id=?";
$query = $this->db->query($sql, array($_newUploadPath, $ID));
return $query;
}
So any ideas?
Codeigniter won't refresh the page when loading a view, so it will never load the images, instead you will need to tell Codeigniter to refresh the page by using the redirect() function. Instead of loading the success view at the end of the do_upload() method, you should do a redirect to your success page.
I would create a upload_big_success() method, to load your upload_big_success_view for you, then redirect to that method.
Replace the following line in your do_upload() :
$this->load->view('upload_big_success_view', $this->data);
with:
redirect(upload_big_success, 'location');
Then create a method for your success page in the same controller.
public function upload_big_success() {
// And this is the view that's called
$this->load->view('upload_big_success_view');
}

Categories