mysql fetch assoc used in url fetching - php

i have a script in which output localhost/urlfromMysqlDatabase
i need output should be urlfromMysqlDatabase
please give me any suggestions to changes this script
$link = mysql_fetch_assoc(mysql_query("SELECT url FROM admins"));
and in between html body i have a code
<a href="<?php echo $link['url']; ?>" target="_self">

Try this:
<a href="<?php echo #end(explode("/", $link['url'])); ?>" target="_self">
end(explode("/", $link['url'])) will print out urlfromMysqlDatabase (everything after the last slash /).

$link = mysql_fetch_assoc(mysql_query("SELECT url FROM admins"));
$link=end(explode("/",$link['url']));
<a href="<?php echo $link; ?>" target="_self">

Related

Codeigniter URI routing is not working

I am using codeigniter framework in my web development and passing the value of image id using php code such below
<a href="<?php echo base_url('account/gallery/view'); ?>?id=<?php echo $imageDetail['imageId']; ?>">
<img src="" />
</a>
and getting the value at controller with this code $imageId = $this->input->get('id');
With the get method I used, it will display the url like this :
http://my-domain-name/account/gallery/view?id=1
But I want to display the url like this :
http://my-domain-name/account/gallery/1
So what I did is setting in the router with this code but it's not working.
$route['account/gallery/(:num)'] = 'default/Gallery_controller/view/$1';
My web still able to show the template of the web if I type http://my-domain-name/account/gallery/1 but display error in the field that I fetch from database because unable to get the imageId
use Regex instead of :num in route.php
$route['account/gallery/([0-9]+)'] = 'default/Gallery_controller/view/$1';
And in your template file:
<a href="<?php echo base_url('account/gallery/'.$imageDetail['imageId']); ?>">
<img src="" />
</a>
Then you can access directly in your gallary controller in view function $id.
eg:
class Gallery extends MY_Controller {
public function view($id){
// Your Image ID = $id;
}
}
Set this url http://my-domain-name/account/gallery/1 proper in href
<a href="<?php echo site_url('account/gallery/view'.$imageDetail['imageId']); ?>">
<img src="" />
</a>
Replace
<a href="<?php echo base_url('account/gallery/view'); ?>?id=<?php echo $imageDetail['imageId']; ?>">
with
<a href="<?php echo base_url();?>account/gallery/view/<?php echo $imageDetail['imageId']; ?>">
Check your config.php
$config['enable_query_strings'] = true;
to
$config['enable_query_strings'] = false;
In this try this (:any)
$route['account/gallery/(:any)'] = 'default/gallery_controller/view/$1';
Or
$route['account/gallery/view?(:any)'] = 'default/gallery_controller/view/$1';
In the routes lower case
This code is make your url like http://domain-name/account/gallery/1
Try this:
<a href="<?php echo base_url();?>account/gallery/view/<?php echo $imageDetail['imageId'];?>">
<img src="" />
</a>

Passing image URL to view from controller

I am currently stuck with a situation where I cant get the right line to pass through to make the image show, this is what I have so far:
Controller
// Get image URL and alternate text
$image_filename = "<?php echo base_url('assets/imgs/'". $code ."'.png'); ?>";
$page_data['image_filename'] = $image_filename;
$image_alt = 'Image: ' . $code . '.png';
$page_data['image_alt'] = $image_alt;
$this->load->view('common/header');
$this->load->view('top_nav');
$this->load->view('shop_viewprod', $page_data);
$this->load->view('common/footer');
}
I have to find the image for the product selected that's why I have to use the $code variable, cause it will always change and that's how I get the code for the current item selected.
And then the view
<img src="<?php echo $image_filename; ?>" alt="<?php echo $image_alt; ?>" />
The path to the image is:
assets\imgs\name.png
The thing is, if I do use this in the view, it finds the image
<img src="<?php echo base_url('assets/imgs/fg700s.png'); ?> " alt="<?php echo $image_alt; ?>" />
Just to show you the two side by side in the code
<img src="<?php echo $image_filename; ?>" alt="<?php echo $image_alt; ?>" />
<img src="<?php echo base_url('assets/imgs/fg700s.png'); ?> " alt="<?php echo $image_alt; ?>" />
and the result:
So I reckon its the way I make the image_filename variable and how I use it in the view, but I cant understand what it is I am missing?
Can anyone help me solve this please?
You are not passing the variable(s) to the view.
When you load your view, you are passing a variable, probably $data, to the $this->load->view() function as the second parameter.
It is probably an array.
You should set a key like $data['image_filename'] = base_url[...]; before loading the view.
So I played around with it alil more and came up with this:
In the controller:
$page_data['code'] = $code;
$image_alt = 'Image: ' . $code . '.png';
$page_data['image_alt'] = $image_alt;
In the View
<img src="<?php echo base_url('assets/imgs/'.$code.'.png'); ?>" alt="<?php echo $image_alt; ?>" />
Works like a charm.

Passing URL does not output variable with echo

The link page:
<a href="displayProduct.php?productID=<?php'.$pID.'?>">
The displayProduct.php page:
<?php
echo $_GET["productID"];
?>
This is my tester, to see if clicking on the link will pass the data from page 1 to displayProduct page.
It successfully passes the variable in the ' URL ' bar but the echo does not display that variable.
Am I missing something?
You missed an echo and have some weird concatenation.
<a href="displayProduct.php?productID=<?php'.$pID.'?>">
should be:
<a href="displayProduct.php?productID=<?php echo $pID; ?>">
<a href="<?php echo 'displayProduct.php?productID=' . $pID; ?>">
The shortest way:
<a href="displayProduct.php?productID=<?php echo $pID ?>">

Append a GET variable on to a page with another GET variable

I have a profile.php page with a variable uid?=12 and i have a language bar on that is on a separate page called lang.inc.php:
Profile Page:
<div class="langbar">
<?php include 'inc/lang.inc.php'; ?>
</div>
lang.inc.php
<center>
<a class="flag_USA" title="English" href="<?php echo basename($_SERVER['PHP_SELF']); ?>"><img src="css/images/us.png"></a> <span> </span>
<a class="flag_France" title="French" href="<?php echo basename($_SERVER['PHP_SELF']); ?>?lang=fr"><img src="css/images/fr.png"></a> <span> </span>
<a class="flag_dutch" title="Dutch" href="<?php echo basename($_SERVER['PHP_SELF']); ?>?lang=de"><img src="css/images/de.png"></a> <span> </span>
<a class="flag_Italy" title="Italian" href="<?php echo basename($_SERVER['PHP_SELF']); ?>?lang=it"><img src="css/images/it.png"></a> <span> </span>
<a class="flag_Italy" title="" href="<?php echo basename($_SERVER['PHP_SELF']); ?>?lang=es"><img src="css/images/sp.png"></a> <span> </span>
<?php echo $_SERVER['PHP_SELF']; ?>
</center>
As you can see im trying to append the ?lang=fr for example to the end of the profile.php?uid=12 so it looks like this profile.php?uid=12&lang=fr but all it's doing is profile.php?lang=fr.
I'm pretty sure $_SERVER['PHP_SELF'] does not include get variables, just the file path, so you'd have to add the uid to the href yourself.
<?php
$basename = basename($_SERVER['PHP_SELF']); // Lets store the basename
$basename_with_uid = $basename . "?uid=" . $_GET['uid']; // and append the uid from the URL. Make sure to do some validation if $_GET['uid'] exists.
?>
<a class="flag_France" title="French" href="<?php echo $basename_with_uid; ?>&lang=fr"><img src="css/images/fr.png"></a>
REQUEST_URI is probably what you are after:
<a class="flag_France" title="French" href="<?php echo $_SERVER['REQUEST_URI']; ?>&lang=fr"><img src="css/images/fr.png"></a>
If your page was profile.php?uid=12, the output should be:
profile.php?uid=12&lang=fr
Ref: PHP $_SERVER variables
edit You may still need to add basename() around REQUEST_URI, depending on what you are doing

passing data using hyperlink in PHP

I'm trying to pass the path of an image ['guest'] from one page to another using a link. (I'm storing URLs of images in database)
Can't seem to get the image to display, which is a bigger image of 'url'. I'm doing it this way so that I can have a larger image displayed in the target page (does_this_work.php) plus adding some other bits on the page too.
I'm still learning and can;t seem to see what I'm doing wrong. Any help appreciated,
<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select databse.
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name) or die("cannot select DB");
$photo=mysql_query("SELECT * FROM `images` ORDER BY (ID = 11) DESC, RAND() LIMIT 7");
while($get_photo=mysql_fetch_array($photo)){ ?>
<div style="width:300px;">
<a href="does_this_work.php?big_image=$get_photo['guest']>" target=""><img src="<?
echo $get_photo['url']; ?>" title="">
</div>
<? } ?>
I then use the following code to try and display the array data in the target file
<?php
echo "this is the page where you get a larger picture of image on previous page, plus
further info";
$big_image = $_GET['guest'];
echo $big_image;
?>
You're missing an opening tag in here (And a closing semi colon, but that's not as problematic here):
<a href="does_this_work.php?big_image=$get_photo['guest'] ?>"
Change to:
<a href="does_this_work.php?big_image=<?= $get_photo['guest']; ?>"
There are several errors in your code. First of all, this is how you use $_GET and $_POST:
ex. site.php?argument=value
To retrieve the value of argument, you need this code in site.php:
//The variable must not necessarily be $value
$value = $_GET['argument'];
//Alt.
$value = $_POST['argument'];
Secondly (like the other answers tell you) you are missing a php-tag here:
<a href="does_this_work.php?big_image=$get_photo['guest']>" target=""><img src="<? echo $get_photo['url']; ?>" title="">
Instead it should be:
<a href="does_this_work.php?big_image=<?php echo $get_photo['guest']; ?>" target=""><img src="<?php echo $get_photo['url']; ?>" title="">
Now, in order to make that compatible with your second code, you need to change the argument sent to guest like this:
<a href="does_this_work.php?guest=<?php echo $get_photo['guest']; ?>" target=""><img src="<?php echo $get_photo['url']; ?>" title="">
OR change the $_GET['guest']; to $_GET['big_image'];
I think I got it all right..
<a href="does_this_work.php?big_image=$get_photo['guest'] ?>" target=""><img src="<?
echo $get_photo['url']; ?>" title="">
You are missing your php starting tags. This should read:
<a href="does_this_work.php?big_image=<? $get_photo['guest'] ?>" target=""><img src="<?
echo $get_photo['url']; ?>" title="">

Categories