OK this is my first post here so, hello! and please go easy on me if this is a simple solution.
I am creating a document database using apache2,mysql and php. I have only had previous experience with html,css and a small amount of javascript so this has been a steep learning curve and a good challenge for me.
I have successfully created the database and uploaded documents into it. i can search and link to the documentment required in a html/php screen but i only seem to be able to use/search, recall one extension type. this would be either .PHP, .PDF, .html, or .docx etc. In reality not all documents are or will be PDF's, below is my script:
/* -- end of connection script --*/
if(isset($_GET['search'])) {
$get_value = $_GET['user_query'];
if($get_value=='') {
echo "<center id='alert'><b>nothing in the search field</b></center>";
exit();
}
if($get_value=='Search...'){
header('Location: index.php');
}
$result_query = "SELECT * from search WHERE site_keywords LIKE '%$get_value%' OR site_title LIKE '%$get_value%' OR site_description LIKE '%$get_value%' ";
$run_result = mysql_query($result_query);
?>
<h3 class="total"><?php echo $row_result; ?>Results for '<?php echo $get_value; ?>'</h3>
<?php
if(mysql_num_rows($run_result)<1) {
echo "<center id='alert'><b>Oops! no search results found</b></center>";
exit();
}
while($row_result=mysql_fetch_array($run_result)) {
$site_id=$row_result['site_id'];
$site_title=$row_result['site_title'];
$site_link=$row_result['site_link'];
$site_description=$row_result['site_description'];
$site_file=$row_result['site_file'];
echo"<div class='results'>
<h3><a href='$site_id.php' target='_blank'>$site_title</a></h3>
<p align='justify'>$site_description</p>
</div>";
}
}
?>
as you can see from the echo results the href file requires an extension (in this case .php) to correctly display a .php document but not other extension types. If i change this to .pdf it will display the .pdf but not .php and so on. I am at a loss.
Related
First posting here. I know inline php is not preferred but I haven't converted all my scripts to echo json_encoded arrays to work in javascript on the client side...so for now, I have inline php.
I do not know the extension of the user uploaded media because it could be a jpg,mp4,etc and upon upload it goes into a media folder with the user id as an identifier.
When my user first loads the div (and html page), the php script cycles through an array and does a fetch_assoc from sql query to the database each time; It returns the (media_id #) and prints out an li with the respective media displayed next to some other values from the query.
I only know the (media_id) and the file path name without the extension. When the page first loads, everything works great and the file_exists function returns correctly.
THE PROBLEM
When I AJAX the div and do the query again, because the user added a row to the database, the new list prints out with all info, BUT the file_exists function doesn't recognize the exact same paths as before and I don't have an img or video on the page.
I copy/pasted the exact same code from the original div and put it in a file for ajax to re-query and print the new li's.
All variables are the same and when I hard code a test filepath, it prints fine. Maybe there's a caching issue?
THE CODE
<?php
$result=$conn->query($select);
$row=$result->fetch_assoc();
?>
<li>
<?php
if ($row['count']>0) {
echo "<div class='media-container'>";
$pathname = "uploads/".$row["id"]."media1";
$testjpg=$pathname.".jpg";
$testjpeg=$pathname.".jpeg";
$testpng=$pathname.".png";
$testmp4=$pathname.".mp4";
if (file_exists($testjpg)==TRUE || file_exists($testpng)==TRUE || file_exists($testjpeg)==TRUE) {
echo '<img src="'.$pathname.'">';
}if(file_exists($testmp4)==TRUE) {
echo "<video></video>";
}
echo "</div>";
}?>
</li>
I could use some advice on how to fix this and how to print appropriate media tags on unknown media types.
THE OUTPUT
<div class='media-container'>
</div>
DEBUGGING ATTEMPTS
echoing the exact file path of a known image in an <img> tag works fine. putting echo'test'; inside the file_exists case does nothing.
--
Solution (Kind of)
So I've used html's onerror before and I found a workaround, though I'd still like to know why I was getting an error. PSA this uses JQuery but javascript works too:
My Solution
<script>
function img2video(el, src) {
$( el ).replaceWith( '<video class="videoClass"><source src="'+src+'" type="video/mp4"></video>' );
}
</script>
<body>
<img style="width:100%" onerror="img2video(this,'<?php echo$pathname;?>')" src="<?php echo$pathname;?>">
</body>
Alright, so here's the final answer I made to best fit the problem using glob:
Javascript:
function img2video(el,src,place) {
if (place=='type') {
$( el ).replaceWith( '<video controls controlsList="nodownload" disablePictureInPicture style="width:100%;object-fit:contain;" preload="auto"><source src="'+src+'" type="video/mp4"></video>');
}
}
PHP:
<?php for ( $i=1; $i <= $limit; $i++) {
$path ="[DIRECTORY]/".$row["id"]."media".$i;
$path = (!empty(glob($path . '*.{jpg,png,jpeg,avi,mp4}', GLOB_BRACE)[0])) ? glob($path . '*.{jpg,png,jpeg,avi,mp4}', GLOB_BRACE)[0] : false;?>
<div>
<img onerror="img2video(this,'<?php echo$path;?>','type',<?php echo$row["id"];?>,<?php echo$i;?>)" src="<?php echo$path;?>">
</div>
<?php } ?>
I don't know how to mark as duplicate, if someone could help with that. My answer uses Glob_Brace from #Akif Hussain 's response on This Question.
I have this IF clause snippet from a PHP script that is basically a small search engine:
if(isset($_REQUEST['search'])){
$search_term = trim($_REQUEST['search']);
if(strlen($search_term) <= 0){
$website_search_dynamic -> error($website_search_dynamic->label("Please enter a search query."),true);
}else if(strlen($search_term) < $GLOBALS['_SEARCH_MIN_CHARS']){
$website_search_dynamic -> error($website_search_dynamic->label("Sorry, you must enter at least %d characters in your search query",$GLOBALS['_SEARCH_MIN_CHARS']),true);
}
$website_search_dynamic -> search($search_term);
}
How do I add CSS styling to the two labels? For example, I would like either error message to be displayed in a centered div. Nothing too fancy.
Thanks!
You could just do:
if(strlen($search_term) <= 0){
$error[] = "Nope";
}
And then wherever you want in your html, put:
<?php
if(!empty($error)) {
echo "<div align='center' class='foo'>" . $error [0] . "</div>";
}
?>
And style the class however you want.
You could even try this method
$website_search_dynamic -> error($website_search_dynamic->label("<span style="text-align:center;color:red">Please enter a search query.</span>"),true);
php can run html codes you can simple add html inline style code to work
try google and find how to add external style to php file if you wish(because i don't know external style is possible in php
I have a website that grabs songs from Soundcloud. My problem is, all of the files that are downloaded have names like aline!
For example, if I download/get file "I walk alone - green day.mp3", after download it looks like bybbydsbaubx.mp3. What to do with it? I just need the song name because all my users are confused.
Here's a sample of my webpage's code.
<?php
include'func.php';
$id=$_GET['id'];
$grab=json_decode(ngegrab('http://api.soundcloud.com/tracks/'.$id.'.json?client_id=c435ec96ae345d5ce32496d339fc291d'));
$duration=format_time($grab->duration/1000);
if(!empty($grab->genre)){
$genre=$grab->genre;
}else{
$genre='Unknown';
}
$name=$grab->title;
$size=format_size(getfilesize(getlinkdl($id)));
if(!empty($name) && !empty($_GET['id']) && !empty($_GET['permalink'])){
$title='Download '.$name.' ('.$size.')';
include'headl.php';
echo'<div class=title align=justify><span xmlns:v="http://rdf.data-vocabulary.org/#"><span typeof="v:Breadcrumb">Home</span> » <span typeof="v:Breadcrumb">'.$genre.'</span> » <span typeof="v:Breadcrumb"><span class="breadcrumb_last" property="v:title">'.$title.'</span></span></span></div><div class="menu"><div class="ml"><b>Download '.$name.'.mp3</b></div><div class="ml">Genre: '.$genre.'</div><div class="ml">Duration: '.$duration.'</div><div class="ml">Size: '.$size.'</div><div class="ml">Bitrate: 128 kbps</div></div>';
$permexp=explode('-',$grab->permalink);
echo'<div class="list">Tag: ';
foreach($permexp as $perma){
echo''.ucfirst($perma).', ';
}
$ttag=clearspace($genre);
$tgrab=json_decode(ngegrab('http://ws.audioscrobbler.com/2.0/?method=tag.getsimilar&tag='.strtolower($ttag).'&api_key=7df2ba528dcd0d495e3db6284ee6e1a3&format=json'));
$tjumlah=count($tgrab->similartags->tag)-1;
if($tjumlah < 4){
$tcount=$tjumlah;
}else{
$tcount='3';
}
if(!empty($tgrab->similartags->tag[0]->name)){
echo'</div><div class="list">Other Genre';
for($i=0;$i<=$tcount;$i++){
echo''.$tgrab->similartags->tag[$i]->name.', ';
}
}
echo'</div><div class=green align=center><img src="/download.gif"><br/><b>Download '.$name.'.mp3</b><br/></div>';
$genrer=str_replace(' ',',',$genre);
$genrer=str_replace('_',',',$genrer);
$genrer=str_replace('-',',',$genrer);
$jsonr=json_decode(ngegrab('http://api.soundcloud.com/tracks.json?genres='.strtolower($genrer).'&limit=8&offset=0&client_id=c435ec96ae345d5ce32496d339fc291d'));
if (!empty($jsonr[0]->title)){
You could just rename the filename. Then on your code, just replace the old filename with the new one you just made.
Consider I have an image named "car.jpg".
I have multi-language stores in my site and client wants to show the same image in various languages. Like car is called voiture in French so the image name should be "voiture.jpg", but it will show the same image i.e. car.jpg.
Also while saving / downloading the image it should get saved as voiture.jpg from the French store and car.jpg from the English store.
I have plans to input the names of the images from the client via a back-end panel, but can’t get ideas about how to implement this in front-end.
Note:- The image is not on my server
I have copied the image on fly and can use it as below, using class upload, but need a better option than coping image as there are above 4000 images.
<img src='./media/scene7/car.jpg' alt='car'>
<?php
include('class.upload.php');
$img = new Upload('media/scene7/car.jpg');
if ($img->uploaded) {
$img->file_new_name_body = 'voiture';
$img->file_auto_rename = false;
$img->file_overwrite = true;
$img->Process('./media/scene7/translated/');
}else{
}
echo "<br /><br />Car is called voiture in French, you are seeing the image of voiture.<br />";
?>
<img src='./media/scene7/translated/voiture.jpg' alt='car'>
<?php
if ($img->uploaded) {
$img->file_new_name_body = 'auto';
$img->file_auto_rename = false;
$img->file_overwrite = true;
$img->Process('./media/scene7/translated/');
}else{
echo "not uploaded";
}
echo "<br /><br />Car is called auto in German, you are seeing the image of auto.<br />";
?>
<img src='./media/scene7/translated/auto.jpg' alt='car'>
<?php
if ($img->uploaded) {
$img->file_new_name_body = 'coche';
$img->file_auto_rename = false;
$img->file_overwrite = true;
$img->Process('./media/scene7/translated/');
}else{
echo "not uploaded";
}
echo "<br /><br />Car is called coche in Spanish, you are seeing the image of coche.<br />";
?>
<img src='./media/scene7/translated/coche.jpg' alt='car'>
<?php
$img = new Upload('http://s7d7.scene7.com/is/image/zeon/Stack_Mouse');
if ($img->uploaded) {
echo 'uploaded image from url';
}else{
echo $img->error;
}
?>
you can have images as blobs in your database and give names as you like it.
pros and cons for storing images as blobs can be find out from the below link
php:Store image into Mysql blob, Good or bad?
Have a database table that lists all the image filenames that you want to display to the user, along with their real URLs. For example, the table might have
+-------------+--------------------------+
|FILENAME |URL |
+-------------+--------------------------+
|car.jpg |./media/scene7/car.jpg |
|voiture.jpg |./media/scene7/car.jpg |
|auto.jpg |./media/scene7/car.jpg |
|truck.jpg |./media/scene7/truck.jpg |
|camion.jpg |./media/scene7/truck.jpg |
|lastwagen.jpg|./media/scene7/truck.jpg |
+-------------+--------------------------+
Now write a page image.php, which expects a filename as a query parameter. It should take the query parameter and look it up in the database table, then do a redirect to the URL that it finds.
You can then include your image in the page as something like
<img src="image.php?file=voiture.jpg" alt="voiture.jpg"/>
When the browser requests this image, your page will be called, and the image will end up coming from .media/scene7/car.jpg.
You can make a hash table like 'car' => 'car.jpg' and 'voiture' => 'car.jpg'. when every time the clients visits voiture.jpg you can return car.jpg.
You can achive this by implemetlnting one cofig file for each language. Here is explanation:-
Suppose you have some images like user.jpg, profile.jpg etc.
Now create one french.php file.
Structure of french.php file:-
<?php
$lang=array();
$lang['user']='french_transalation_of_user';
$lang['profile']='french_transalation_of_profile';
?>
In same way add all your image names which you want to display in french. Now call these names dynamically in webpage. You can also create as many as language.php as you want.
I hope you understand the logic. Sorry for bad english.
I am writing a script to display images on my site. The images are stored on the server and the source (path) is on a mySQL database. I want to be able to move to the next (or previous picture) by invoking two php functions I have made to do so. The code I have written so far is:
<?php
require "db_connection.php";
$query="SELECT source FROM photos";
$result=mysql_query($query);
$count=0;
$numberofpics=mysql_num_rows($result);
$image=mysql_result($result, $count, "source");
$num=1;
function slideshowForward() {
$num=$num+1;
if($num==($numberofpics+1)) {
$num=1;
}
$count=$count+1;
$image=mysql_result($result, $count, "source");
}
function slideshowBack() {
$num=$num-1;
if ($num==0) {
$num=$numberofpics;
}
$count=$count-1;
$image=mysql_result($result, $count, "source");
}
?>
The html portion to display the images is:
<!-- FORWARD AND BACK FUNCTIONS-->
<a class="back" href="http://mywebsite.com/discoverandrank.php?function=slideshowBack()"> <img src="graphics/back.png"/></a>
<a class="next" href="http://mywebsite.com/discoverandrank.php? function=slideshowForward()"><img src="graphics/forward.png"/></a>
<!--DISPLAY MIDDLE PHOTO-->
<div id="thepics">
<img src="http://www.mywebsite.com/<?php echo $image; ?>" name="mypic" border=0 height="300" width="500"></img>
</div>
I'm pretty sure that the php script is incrementing/decrementing the count currently for the image, but I think the problem might be because the html (specifically img src="....") part is not re-evaluated when the count of the image increases?
Mmm there are two things to point out here:
I think that passing a function name as a parameter on an action is not a good idea, instead you should user something like: "http://mywebsite.com/discoverandrank.php?op=next¤t=10" and then evaluate the op and current value to take some action.
For gettting the next picture from database you could use something like this:
"Select source FROM photos LIMIT "current+1" , 1"
For gettting the previous picture from database you could use something like this:
"Select source FROM photos LIMIT "current-1" , 1"
This way you dont need to keep counters and stuff, just have to check if those queries return any data.
Hope this helps!