Attaching a number to the end of variable php - php

Probably another basic question but its been annoying me for a while now...
I have a php file which is included on a php web page to bring in dynamic content from my mysql database.
Everything works fine with this except when i try get pictures to work and here is the problem.
I am using the code:
echo "<img src=fishery_images/$region/$url/$url1.jpg'/>";
All of which has been selected from the correct table and so on. an example of what i want this to resolve to is below:
fishery_images/fife/goldenloch/goldenloch1.jpg
However because i have the code:
$url1.jpg
and $url1 is not defined as anything then it resolves as the following:
fishery_images/fife/goldenloch/.jpg
i can have anything from goldenloch1.jpg all the way to goldenloch10.jpg so need to be able to say which image should be used.
how is it i say $url1.jpg without meaning $url1?
Really hope this makes sense... and thanks in advance

echo "<img src=fishery_images/{$region}/{$url}/{$url}1.jpg'/>";
http://en.wikipedia.org/wiki/String_interpolation#PHP

You'll want to use curly braces to wrap your variable:
echo "<img src=fishery_images/$region/$url/{$url}1.jpg'/>";

Related

are line breaks possible in esc_html_e()?

I am using the following code:
<p><?php esc_html_e( 'It looks like nothing was found at this location. Maybe try one of the navigation links above or a search? Or it is possible you are trying to access a restricted page without being logged in to gain access.', 'shapely' ); ?></p>
Which ends up being displayed as follows:
It looks like nothing was found at this location. Maybe try one of the navigation links above or a search? Or it is possible you are
trying to access a restricted page without being logged in to gain
access.
What I want to display is this:
It looks like nothing was found at this location.
Maybe try one of the navigation links above or a search?
Or it is possible you are trying to access a restricted page without
being logged in to gain access.
So what I want is a way to break the line. I tried <br /> and \n, and neither work. Is there a way to add line breaks in the esc_html_e() function?
I know this a older question, I wanted to to point out that there is no need to use the output buffering functions in PHP. You can simplify this code by calling the WordPress function esc_html__() instead of esc_html_e(). This function will return the escaped value instead of echoing it out.
https://developer.wordpress.org/reference/functions/esc_html__/
echo nl2br( esc_html__("Line1\nLine2\nLine3", 'shapely') );
First off you can't use \n inside single quotes. Also, line breaks won't be rendered as such on on a webpage. From the looks of things esc_html_e() actually echoes the output so to capture & process it you'd need to do something like this:
ob_start();
esc_html_e("Line1\nLine2\nLine3");
$output = ob_get_contents();
ob_end_clean();
echo nl2br($output);
But this seems like an awful lot to go through and probably the wrong way to go about it. If you need to output html you probably don't be using esc_html_e() to begin with. Really hard to say without more context.

php get the php name from the url

I have these types of urls
something1/anotherThing1/View/fileName.php
something2/anotherThing2/View/fileName2.php?variable1=value1&variable2=value2
...
I want to get the fileName without the .php
I tried this
$_SERVER['REQUEST_URI']
but it seems it gives me the whole url.
Maybe I need a regualr expression.
Could you help me please. i am not good in PHP and i just need this small issue.
appreciate it
echo basename($_SERVER["SCRIPT_FILENAME"]); //with php
echo '<br>';
echo basename($_SERVER['REQUEST_URI'], ".php");//without php
check this answer
Get the current script file name

mysql fetch array returns false in function but not in the document

i am creating a CMS and have php creating a page. i have a while loop like this
while($row = mysql_fetch_array($results)) {
echo "some html code" . $row['name'];
its shortend but hopefully you get the point. i have the full thing in my page working just as it should and i wanted to move it to a function include as i want to reuse it. the problem is i do that and it stops working.
i did some testing and found that the function is getting the query result and after doing a var dump both were identical the problem comes when i try to assign it to an array. it comes back as false so in the above code, for example,
$row = false;
im toatly lost in this and if my explanation is confusing i appologise but i am a bit of a newbie i have tried searching but....i dont really know where to begin
any thoughts.
the query you are doing is basically wrong, try posting exactly the code which you have in $query and then let us see the problem.
also, it is better to use mysqli functions.
but for this, edit the question and type the query, or simply put a die(mysql_error()) at the end of your query which is in $query. It will show your exact error.
i fugured it out
when i was testing the function i commented out the original code on the main page but for some reason i had not comented out enough (it was a mix of php and html clearly the php had not been commented out properly) this must have been causing a clash of some kind as when i put the function above the code on my page the function worked and the long code below it did not
sorry for wasting your time guys

Paste terms in URL

I am trying to find a way to create a simple dynamic URL, that gets its information from boxes where people enter something.
I got a google search machine and want to refer to it, so basically I wanted two boxes:
One for choosing which directory to search in (the google machine has different directories in its index I want people to be able to choose from those)
and the other for the search term they are looking for.
The URL looks like that:
http://searchengine.xx/search?q=SEARCHTERM&site=DIRECTORY&btnG=Suchen&entqr=0&ud=1&sort=date%3AD%3AL%3Ad1&output=xml_no_dtd&oe=UTF-8&ie=UTF-8
I tried it with PHP like that:
<?php
$directory = $_GET['searchterm'];
echo "http://searchengine.xx/search?q=".$searchterm."&site=directory&btnG=Suchen& entqr=0&ud=1&sort=date%3AD%3AL%3Ad1&output=xml_no_dtd&oe=UTF-8&ie=UTF-8'>URL</a>
?>
This doesnt seem to work well and I wondered if this was possible in any other way (simple HTML, JavaScript maybe?)
try to mix php and htaccess...
<?php
//get the text form the text box and put it in a variable eg.($text)
$url = 'index.php?searchterms=$text';
header("Location: $url");
?>
I think that something like this might work.
Get the text from the text box, then put the text onto a variable (i've used $text to exemplify).
Put the url that you want in a variable (i've used $url to exemplify), but in the end of the url put the text variable the way i did it.
Finally, use the header function to redirect to the url that you want.
Hope i helped
There are several problems with the PHP code in your question. $searchterm is never set and the echo statement is never ended. Try this instead:
<?php
$searchterm = $_GET['searchterm'];
$searchterm = strip_tags($searchterm);
echo "<a href='http://searchengine.xx/search?q=".$searchterm."&site=directory&btnG=Suchen&entqr=0&ud=1&sort=date%3AD%3AL%3Ad1&output=xml_no_dtd&oe=UTF-8&ie=UTF-8'>URL</a>";
?>
The strip_tags will ensure " and ' are removed so it doesn't break your link.

Image upload form not working with PHP

I have this form for multiple images here:
http://www.comehike.com/account/upload_hike_photos.php?hike_id=58
And when I try to get the images in the POST processing of the request, I get these messages:
Uninitialized string offset
This array does not get populated:
$_FILES['uploadedfile']['name']
Any idea why? I have a very similar form like this working here:
http://www.hikingsanfrancisco.com/account/upload_hike_photos.php?hike_id=58
But I can't quite figure out what the problem might be that makes the array of images be empty.
I think you need to end the name="uploadedfile" with a bracket so: "uploadedfile[]". Then you can go through the $_FILES['uploadedfile'] with foreach and put them in your server just like the other form.
EDIT: I've checked the source of your second link too, you have brackets in the second page you've provided. :)
I find dumping the contents of the array often helps see the structure. Try:
<?php echo '<pre>'.print_r($_FILES, true).'</pre>; ?>
However, that said, I think Tekin was on the right path. Try using a foreach or accessing the files directly like:
$_FILES['uploadedfile'][0]['name']
$_FILES['uploadedfile'][1]['name']
$_FILES['uploadedfile'][2]['name']

Categories