Jump to content

Javascript inside php...or php inside java


bschultz

Recommended Posts

I'm trying to pull results from a database (using php) to populate a javascript "top news rotation" script I found.

 

The problem is that I'm getting the "java is disabled" message that is in the code...instead of the results I'm expecting.

 

Here's the code:

 



<!-- create a element in your HTML like the following -->
<div id="quotetext" >
Text will go here. Be sure to add initial text here for users with JavaScript disabled.
</div>


<!-- The easiest way is to place the below JavaScript code after the above HTML. The better
     way would be to add in the <head> section of the document and call the rotatequote() function through the window.onload event. However this can cause problems if you have other scripts that use the 
     window onLoad settings -->
<script type="text/javascript" >
var myquotes = new Array(


<?php
$link = mysql_pconnect($host, $username, $password);  
mysql_select_db('briansch_brn',$link);
$sql = "SELECT * FROM story WHERE ORDER BY date DESC LIMIT 4";
$rs = mysql_query($sql,$link);  
$matches = 0; 
while ($row = mysql_fetch_assoc($rs))  {
$matches++;

echo "'<strong>$row[headline]</strong><br />(posted $row[date]) - $row[short_story]<br /><a href='/pages/$row[keyword]'>READ MORE</a>'";

if($matches < 4) {
        echo ',';
        }
if($matches == 4) {
        echo '';
        }



}  
if (! $matches) { 
echo (""); 
}  
echo "";  
?> 

);

function rotatequote()
{
thequote = myquotes.shift(); //Pull the top one
myquotes.push(thequote); //And add it back to the end

document.getElementById('quotetext').innerHTML = thequote;
// This rotates the quote every 10 seconds.
// Replace 10000 with (the number of seconds you want) * 1000
t=setTimeout("rotatequote()",10000);
}

// Start the first rotation.
rotatequote();
</script>

 

I know that javascript inside php is hard to make work...and I know that php inside javascript is hard to make work.  Any ideas?

 

Thanks!

Link to comment
Share on other sites

The problem is that I'm getting the "java is disabled" message that is in the code

 

What Java is disabled message? Your not even using Java. Java and javascript are not the same thing.

 

I know that javascript inside php is hard to make work

 

I'm not sure where you heard that.

 

I know that php inside javascript is hard to make work

 

In fact is not possible because js executes on the client, long after php is done.

 

You need to view your html source. The javascript array you are creating is broken for sure, but i'm not sure that is your actual problem as yet.

Link to comment
Share on other sites

I'm getting the following error:

 

Text will go here. Be sure to add initial text here for users with JavaScript disabled.

 

 

When viewing the page source, I'm getting an error on line 26...which is the blank line between these two lines:

 

$matches++;

 

echo "'<strong>$row[headline]</strong><br />(posted $row[date]) - $row[short_story]<br /><a href='/pages/$row[keyword]'>READ MORE</a>'";

 

Warning</b>:  mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in <b>news.php</b> on line <b>26</b><br />

Link to comment
Share on other sites

I did a little more research...and I echo'd all javascript code in php fashion...

 

<?php
require_once "constants.php";

echo "<!-- create a element in your HTML like the following -->\n";
echo "<div id='quotetext' >\n";
echo "Text will go here. Be sure to add initial text here for users with JavaScript disabled.\n";
echo "</div>\n";


echo "<!-- The easiest way is to place the below JavaScript code after the above HTML. The better way would be to add in the <head> section of the document and \n call the rotatequote() function through the window.onload event. However this can cause problems if you have other scripts that use the window onLoad settings -->\n";


echo "<script type='text/javascript' >\n";
echo "var myquotes = new Array(\n";


$link = mysql_pconnect($host, $username, $password);  
mysql_select_db('briansch_brn',$link);
$sql = "SELECT * FROM stories ORDER BY date DESC LIMIT 4";
$rs = mysql_query($sql,$link);  
$matches = 0; 
while ($row = mysql_fetch_assoc($rs))  {
$matches++;

echo "'<strong>$row[headline]</strong><br />(posted $row[date]) - $row[short_story]<br /><a href='/pages/$row[keyword]'>READ MORE</a>'";

if($matches < 4) {
        echo ",\n\n";
        }
if($matches == 4) {
        echo "\n\n";
        }



}  
if (! $matches) { 
echo (""); 
}  



echo ");\n";

echo  "function rotatequote()\n";
echo "{\n";

echo "thequote = myquotes.shift(); //Pull the top one\n";
echo "myquotes.push(thequote); //And add it back to the end\n";

echo "document.getElementById('quotetext').innerHTML = thequote;\n";
echo "// This rotates the quote every 10 seconds.\n";
echo "// Replace 10000 with (the number of seconds you want) * 1000\n";

echo "t=setTimeout('rotatequote()',10000);\n";
echo "}\n";

echo "// Start the first rotation.\n";

echo "rotatequote();\n";
echo "</script>\n";

?> 

 

I viewed the page source...and it appears to be identical to "straight" javascript code...and it still doesn't work...same error message as before.

Link to comment
Share on other sites

Wait... You are getting a javascript is disabled message... Yet you are not using a noscript tag? I'm confused... Do you mean that you actually have javascript disabled on your browser? In that case I want to be the first to say... No shit it doesn't work lol.

 

In the other case that you do have javascript enabled then you may get better luck posting this in the javascript forums. Or if you want help here you should say exactly what is happening and like thorpe said provide the html that the code is producing so that we can look at a clean version of what you are trying to do and better assess any problems. Have you by chance used firebug or the internet explorer javascript error feature? If so what kind of errors are you getting on there?

Link to comment
Share on other sites

No, I have not disabled Javascript in my browser!

 

Here is the page source:

 

<!-- create a element in your HTML like the following -->
<div id='quotetext' >
Text will go here. Be sure to add initial text here for users with JavaScript disabled.
</div>
<!-- The easiest way is to place the below JavaScript code after the above HTML. The better way would be to add in the <head> section of the document and 
call the rotatequote() function through the window.onload event. However this can cause problems if you have other scripts that use the window onLoad settings -->
<script type='text/javascript' >
var myquotes = new Array(
'<strong>Beaver Football Picked Fifth</strong><br />(posted 2010-08-10 21:21:10) - <img src=\"http://www.bsubeavers.com/media/2010-11/football/footballpractice.jpg\" /><br />After posting a 7-3 record in conference play during the 2009 season, the Bemidji State University football team has been tabbed fifth (122 points) in the 2010 Northern Sun Intercollegiate Conference (NSIC) Preseason Coaches’ Poll announced today by league officials. In addition, BSU senior quarterback Derek Edholm (Anoka, Minn.) and senior defensive end Jordan Lardinois (New Franken, Wis.) were recognized as preseason players to watch.<br /><br /><a href=\"/pages/fbpickedfifth/\">READ MORE</a><br /><a href='/pages/fbpickedfifth'>READ MORE</a>',

'<strong>Football Team In Pads</strong><br />(posted 2010-08-10 21:17:32) - <img src=\"http://www.bsubeavers.com/media/2010-11/football/footballpractice.jpg\" /><br />The BSU Football Team has already begun preparations for the August 26th opener against Minot State.  <br /><br /><a href=\"/pages/fbinpads/\">READ MORE</a><br /><a href='/pages/fbinpads'>READ MORE</a>',

'<strong>New Website Launched</strong><br />(posted 2010-08-10 21:16:32) - <img src=\"/static_images/newwebsite.jpg\" /><br />We have launched a new website for the Beaver Radio Network!  Here, you will get updates on the Beavers, including blogs, interviews...and of course, live play-by-play of games.  <br /><br /><a href=\"/pages/newweb/\">READ MORE</a><br /><a href='/pages/newweb'>READ MORE</a>',

'<strong>Youth Football Camp Offered</strong><br />(posted 2010-08-09 21:13:57) - <img src=\"/static_images/youthfootball.jpg\" /><br />The Beaver Radio Network, along with Pepsi Nei Bottling and the BSU Beavers Football team are sponsoring a youth football camp on August 28th.  Visit <a href=\"http://kkbj.com\">KKBJ - Mix 103.7</a> to register.<br /><br /><a href=\"/pages/footballcamp/\">READ MORE</a><br /><a href='/pages/footballcamp'>READ MORE</a>'

);
function rotatequote()
{
thequote = myquotes.shift(); //Pull the top one
myquotes.push(thequote); //And add it back to the end
document.getElementById('quotetext').innerHTML = thequote;
// This rotates the quote every 10 seconds.
// Replace 10000 with (the number of seconds you want) * 1000
t=setTimeout('rotatequote()',10000);
}
// Start the first rotation.
rotatequote();
</script>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.