Hey guys I'm having a bit of trouble trying to make my SQL column a variable in PHP.
So my SQL row contains image URL's and are formatted like this:
http://www.website.com/image.jpg,http://www.website.com/image2.jpg,http://www.website.com/image3.jpg
So now I need to display the first URL in the row.
I have this short line of code:
file = '.(explode(',', $cardata["PictureRefs"])[0]).';
I am basically trying to give the variable a value of:
$file = "http://www.website.com/image.jpg"
My current code is obviously wrong however I feel like I must be quite close. How can I achieve this?
Your short hand is correct, but the string encapsulation is simply not required.
$file = explode(',', $cardata['pictureRefs'])[0]; // first image SRC
Then you can utilize it like:
echo '<img src="'.$file.'"/>';
No problem.
$file=explode(',',$cardata)[0];
Try this:
$file = explode(',',$cardata);
$img = $file[0];
Related
Good day,
I would like to take screenshots from a ESP32 cam and save it on my NAS. I put the following script on my nas, the picture is saved but the name of the picture is:
always the same which means that the next picture will overwrite the previous one
is a funny name like _OT3HJ~ (and not a date with time as I would have expected...)
Can someone help me a little?
Thanks and best regards,
Yannick
<?php
$url = 'http://192.168.1.232:80/capture';
$img = '.new date("dmYHis").capture.jpg';
file_put_contents($img, file_get_contents($url));
?>
PHP does not allow you to execute functions inside of string definitions like that. Luckily there are are numerous ways to solve this problem.
Simply concatenate the date result and the rest of the file name
$img = date('dmYHis').'capture.jpg';
Use complex syntax to insert the date variable in to the file name string
$date = date('dmYHis');
$img2 = "{$date}capture.jpg";
Use sprintf to build the string
$img3 = sprintf('%dcapture.jpg', date('dmYHis'));
I want to get the last part of an url that looks like this:
http://localhost:8888/blog/public/index.php/categories/Horror
I've tried it with
$endOfUrl = end(explode('/',$url));
but the thing is I get a notice that "Only variables should be passed by reference"
I need this "Horror" to get it's ID in my database and get all the posts with this id, since I'm trying to code a blog to get experience with php.
Another question linked to this: Is it possible to make it dynamic so it can be used for all the other categories as well? Or do I have to do this for every single category?
I'm new to the world of php so I would really appreciate it if someone could help me on this.
Try like this way for end() but If I were you I will try basename() to get my job done.
<?php
$url = 'http://localhost:8888/blog/public/index.php/categories/Horror';
$exploded = explode('/',$url);
$endOfUrl = end($exploded);
echo $endOfUrl;
?>
Reason why it is not working on single line:
end() requires a reference, because it modifies the internal
representation of the array (i.e. it makes the current element pointer
point to the last element).The result of explode('.', $url) cannot be
turned into a reference and this is a restriction in the PHP language itself.
DEMO: https://3v4l.org/ttKui
Using basename(),
$url = 'http://localhost:8888/blog/public/index.php/categories/Horror';
echo basename($url);
DEMO: https://3v4l.org/pt2cQ
I have a query string in the URL:
?search=whatever
I need to append this to paginated links:
example.com/articles/p3/?search=whatever
I can get the value using:
$_GET['search'] //whatever
But what's the best way to build the string so I can append to my URL. Is there a proper way or would it just be a case of appending a string:
'?search='.$_GET['search']
For building url's you always should use propper urlBuilder.
For PHP you can use parse-url and http-build-query.
If it's only one variable you are going to use on that page it is ok to use
'?search='.$_GET['search']
But you should also make sure that you care for encoding that var with urlencode() somewhere in your code (where the var is initially set up).
If you want to build an URL with multiple args there is a nice little function called http_build_query() which can help you with that.
Yeah just do
$search = $_GET['search'];
$pagecount = 10; //Count of results from DB or similar pagination data. ie skip take etc..
for(i=0;i<$pagecount;i++){
echo "<a href='getmoreresults.php?search=".$search."&page=".$i."'>$i</a>";
}
PHP beginner's question.
I need to keep image paths as following in the database for the admin backend.
../../../../assets/images/subfolder/myimage.jpg
However I need image paths as follows for the front-end.
assets/images/subfolder/myimage.jpg
What is the best way to change this by PHP?
I thought about substr(), but I am wondering if there is better ways.
Thanks in advance.
you should save your image path in an application variable and can access from both admin and frontend
If ../../../../ is fixed, then substr will work. If not, try something like this:
newpath=substr(strpos(path, "assets"));
It might seem like an odd choice at first but you could use ltrim. In the following example, all ../'s will be removed from the beginning of $path.
The dots in the second argument have to be escaped because PHP would treat them as a range otherwise.
$path = ltrim('../../../../assets/images/subfolder/myimage.jpg', '\\.\\./');
$path will then be:
assets/images/subfolder/myimage.jpg
I suggest this
$path = "../../../../assets/images/subfolder/myimage.jpg";
$root = "../../../../";
$root_len = strlen($root);
if(substr($path, 0, $root_len) == $root){
echo substr($path, $root_len);
} else {
//not comparable
}
In this way you have a sort of control on which directory to consider as root for your images
I'm having this problem that's driving me nuts.
I've got a MySQL database in which there is a table that contains a text field. I am querying the table in PHP and trying to put the content of the text field for each row into a var.
I am doing something like this:
for ($i=0;$i<$nbrows;$i++){
$id = $data[$i]['ID'];
$description = $data[$i]['DESCRIPTION'];
$mystring .= '<div>'.$id.': '.$description.'</div>';
}
DESCRIPTION is my text field.
I'll pass on the details. The $data array is built from mysql_fetch_array($result). I also tried using objects instead, as I use mysql_fetch_object for all my other routines, but there is no change.
Anyway, the problem is this: if I do "echo $description;" then it works. I am getting my text field data as expected. The problem is that I don't want to output it directly, but add it to a concatenated string, and that is not working. What happens in that case is it seems to be taking $description for some kind of array or object. To make the above example work, I have the replace the string with:
$mystring .= '<div>'.$id.': '.$description[0].'</div>';
So in the concatenated string code, if I treat $description as an array, it works, but obviously I am getting only one letter. (it doesn't actually seem to be an array because I can't implode it).
I tried a million things but I just can't make this work unless I use echo, but that is not what I am trying to do.
There is no issue with fields that aren't text.
Thanks for any ideas!
There is nothing visually wrong with the code you pasted, maybe if you could also add the fetching function as well, we might be able to help you further.
Maybe you could post a var_dump of your $data array?
Have you tried $mystring .= "<div> $id : $description </div>";
Ack, well, you know, hours spent on this and then it becomes obvious after I decide to post for help. This is just because of text encoding/escaping and nothing else. I just didn't see well enough where the problem was actually happening.
Thanks for taking the time to read and respond!