clear the div and reload a new audio content in that - php

I have a database which contains the link to audio files. I am trying to play them one after the other using the HTML audio tag. Presently I am using PHP. My code -
$sql = "SELECT * FROM table";
$r = mysql_query($sql);
while($row = mysql_fetch_array($r)) {
$id = $row["audID"];
$audsq = "SELECT * FROM table2 WHERE id ='$id'";
$resultaud = mysql_query($audsq);
//Here I want to write a code to clear div="test"
PlayAud(resultaud);
sleep(2); ///Not Sure
}
I am passing the link to aud file for the html to load and in
<?php
PlayAud($res) {
$raud = mysql_fetch_array($res); ?>
<div id="test">
<audio> ... </audio>
</div>
<?php }?>
Can some one tell me if I am doing it right ? also each clip is of 2 seconds hence I need to wait in the while loop for 2 seconds. If there is any other way can you suggest me?
Thank You

You cannot clear a div from php. The reason is that php is running on the server, and the div is echoed out to the client, where it is unreachable by php.
A solution here could be to use Client side scripting (with JavaScript). You'd program a function, which replaces the audio tag or its contents, and it would probably be a good idea to implement some preloading-mechanism as well.
Greetings,
Jost

You can NOT do what you want in PHP. You will have to use javascript. This question will help you:
Update content of div auto

Hmm.... to clear to div you have to let the browser handle it, and you can do that through javascript.
echo "<script>document.getElementById('test').innerHTML = '';</script>";
//Then do what you needed to do

Related

Load more rows from database on click without page refresh

I am trying to have my loop stop at 5 rows by default, but when the button is clicked it would load more. Are there any simple solutions for this? I don't want the page to refresh either.
My code below:
<?php
$count = 0;
while (($row = mysql_fetch_assoc($thequery))) {
echo $row['title'];
$count+=1;
if($count%5==0){
break 1;
}
}
?>
<span onClick="loadmore_somehow">Load More</span>
An AJAX call would do that. Using Javascript (and preferably a library like jQuery) you would make a call to your script and get more rows, pass them back, and then use Javascript to draw the rows. This does not reload the page.
http://api.jquery.com/category/ajax/
I think you need to know more about Javascript Ajax, It will help you do update,retrieve the content without refreshing page.
Check out this tutorial http://www.developphp.com/view.php?tid=1350

PHP script reloading

I have a file which is of php type. And I have a combination of HTML elements, javascript functions and some PHP scripts as well. I want to rerun the php script say some part of the script again and again. lets take this example:
<html>
<?php
$connection = mysql_connect('localhost','root','root');
$db = mysql_select_db('messenger');
if ($db == null)
{
echo "hello";
}
$messagecheck = "select * from Messages where destination = '$user' && status = 'ACTIVE'";
$result = mysql_query($messagecheck);
$no_rows = mysql_num_rows($result);
............
?>
<body>
........
<form>
<input type="submit">
.......
</form>
</body>
</html>
I want to run the above php script continuously for every 1 min from the same file. When I make use of location.reload() I find that complete document gets reloaded. I just want the affect the part of the page which php script accesses and not the whole doc.
How can I do that? Please help.
You can't do that out of the box, since php is called when the page is loaded.
You should take a look at Ajax or frameworks like JQuery, that embed Ajax.
As previously said by blue112 you need to use Ajax in order to get what you want.
If I did understand well what you need, a simple solution is using an iframe where you point to a file with only the php code you want to execute, and reload it every time you want.
I hope this helps

Swap images from PHP array with Ajax and also get ID

On my website an user is able to fill in an url. When he fills in the url, he gets all the images src's from that url. I push these src's to an array in php:
array_push($goodfiles,$pic);
Now the user will be able to choose on of the pictures (with a next or prev button) and then save it to the database. The picture that's saved is based on the id of the image in the array. So $goodfiles['0'] means id = "0" and so on.
I want the swapping of the images to work with ajax, so that the pages doesn't have to refresh all the time when clicking the next or previous button. And then when I save the form, I want to know the id of the current image, so that I can save it to the database.
How do I realize this with Ajax (jquery)?
Edit:
This is how I do it right now:
$current_id = $_GET['id'];
if(empty($_GET['id']) || !empty($empty)) { $current_id = 0; }
$prev_id = $_GET['id'] - 1;
if($prev_id < 0){ $prev_id = 0;}
$next_id = $_GET['id'] + 1;
if($next_id > $_SESSION['count']-1 && $_SESSION['count'] != 'empty') { $next_id = $_SESSION['count']-1;}
This is the code for the pagination
And this is the pagination:
<div id="url_pic">
<img src="<?=$_SESSION['pictures'][$current_id]?>" class="img_load"><br>
<? if($_SESSION['count'] > 1) { ?><center><img src="img/add/left.png"> <img src="img/add/right.png"></center> <? } ?>
</div>
So right now my solution doesn't contain any javascript, but it's all php coded. And the page refreshes everytime you want to see the next picture. I want to solve this in ajax, so that you can paginate through the images without a refresh. The way I want it is like this link:
http://d-scribe.de/webtools/jquery-pagination/demo/demo_options.htm
But except for the text, I want to paginate through images.
You probably don't need to use AJAX for this. Simply return a html file containing a JavaScript array, which contains all those image URLs and do the other stuff using JavaScript.
Get back to StackOverflow in case you've a more precise question and hopefully some code, which we can help on ;)
Load a script at the bottom of your php page the user side of the PHP where all your HTML is, above the closing body tag thats something loosely similar to this
<script type="text/javascript">
var myArray = <?php echo json_encode($myPHParray); ?>
</script>
this way when your page loads out it renders with a dynamic javascript json object as a variable that you can work with client side, this removes the need for an AJAX request all together unless your doing stuff with the data your playing with. From first glance Im guessing not really per say. But yea, at the very least its one less transaction to be made when the page is loading.
edit just noticed someone said similar while I was typing out.. Lars.. so I guess this is a follow up to his answer :-D

using jquery .ajax or .get to receive content from database via php script

I have this php script to communicate to mysql right now, and it displays in tab number 2:
$res = mysql_query("SELECT K_id, title FROM headings WHERE
MATCH (title) AGAINST('$kquery') ORDER BY
MATCH (title) AGAINST('$kquery') DESC");
while ($result = mysql_fetch_array($res)){
$kid= $result['K_id'];
echo "<h1>{$result['title']}</h1>";
$content = mysql_query("SELECT content, url, id FROM kparagraphs WHERE K_id = '$kid' LIMIT 3");
while ($con = mysql_fetch_array($content)){
echo $con['content'];
}
}
what I'm also doing is actually selecting/loading content into the database in tab number one. I wanted the content to be displayed in tab number 2 as it is being loaded via ajax. How could I do this in jquery with php?
how would I pass data to the file ...the php script starts with $kid = $_GET['title']....title is enocoded in the url. How can I pass this along using the mentioned answer that uses .load?
1.) Create the page you want loaded via ajax using your code above and echo out the result.
2.) Create two div's for your content that you can later turn into tabs if you want, assign their id values as mydiv1 and mydiv2
3.) At the top of the page with div1 and div2 make sure you have the jquery script loaded and then add some code like:
<script type="text/javascript">
$('div#mydiv2').load('url_to_the_file_you_created_with_div_2_content');
</script>
Try using :
$.ajax({url:'page.php',success:function(data){
// play with data
}});
If I understand your request...this should work:
$.get("thescript.php", function (html) {
$("#tab2").html(html);
}

creating a popupwindow from html, not a file

I have a php page that displays rows from a mysql db as a table. One of the fields contains HTML markup, and I would like to amke this row clickable and the html would open in a new popup window. What is the best way to do it, and is there a way to do it without writing the html to a file?
edit: this php page is actually part of an ajax app, so that is not a problem. I do not want to use jquery, as I would have to rewrite the application.
edit:
I have tried this again using the example below, and have failed. I know my script tag is wrong, but I am just echoing out row2 at the moment, so I think my logic is wrong before it ever gets to the javascript.
$sql="SELECT * FROM Auctions WHERE ARTICLE_NO ='$pk'";
$sql2="SELECT ARTICLE_DESC FROM Auctions WHERE ARTICLE_NO ='$pk'";
$htmlset = mysql_query($sql2);
$row2 = mysql_fetch_array($htmlset);
echo $row2;
/*echo '<script> child1 = window.open ("about:blank")
child1.document.write("$row2['ARTICLE_DESC']");
child1.document.close()*/
child1 = window.open ("about:blank")
child1.document.write("Moo!");
child1.document.close()
If you don't require a browser window (this suggestion involves a css modal box), you might want to consider one of the "lightbox" variations.
Thickbox can be used to load inline content from the page into a modal window. The link provides demo/details in how to implement inline content as well as other variations.
I would suggest using a Javascript library like jQuery since it simplifies this type of code and is cross browser compatible.
$(document).ready( function() {
$('#clicker').click( function() {
myWindow=window.open('','','width=200,height=100')
myWindow.document.write($("#content"));
return false;
});
});
The html will be:
<table>
<tr>
<td><div id="clicker">Click Here</div></td>
<td><div id="content">This is the content</div></td>
</tr>
</table>
if its a lot of data, worth thinking about loading it up via ajax when viewed to save selecting it and sending it to the user on every page load - which would be fairly easy with something like jquery as suggested already.

Categories