1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

javascript and php

Discussion in 'JavaScript' started by vlad230, Dec 29, 2007.

  1. #1
    Hello!

    I modified a javascript script (that displays some random text from an array) with some php to get the data out of the database and it seems like the array doesn't work if I put more than 200 characters into an element of the array... (to be honest I kinda expected this to happen :p )

    Now, I want your help on how to modify it to accept like 2000 characters :D

    I'm not that experienced with javascript but I remember that in C++ or pascal you could declare an array of as many characters as you want, isn't this possible here?

    Here's the script(in the <head></head> section):

    
    <SCRIPT type="text/javascript">
    var quotations = new Array()
    <?
    include("dbconnect.php");
    $getnews = mysql_query("SELECT * FROM rand_news ORDER BY id ASC");
    while($r=mysql_fetch_array($getnews)){
    extract($r);
    
    echo("quotations[$id] = \"
    <table width=\"560\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
    <tr>
    <td width=\"206\">
    	<a href=\"$link\"><img src=\"$src\" border=\"0\" alt=\"$alt\" /></a>
    </td>
    <td width=\"354\">
    	<p><a href=\"$link\" class=\"link\">$title</a><br />
    	($date)<br /><br />
    	$message<br />
    	<b>$img_vid</b> 
    	</p>
    </td>
    </tr>
    </table>
    \";
    
    ");
    }
    ?>
    function display()
    {
    a=Math.floor(Math.random()*quotations.length)
    document.getElementById('quotation').innerHTML=quotations[a]
    setTimeout("display()",5000)
    }
    </SCRIPT>
    
    PHP:
    Any HELP is GREATLY APPRECIATED! :)

    Cheers,
    Vlad
     
    vlad230, Dec 29, 2007 IP
  2. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't think there is such limitation, because something like this works fine, even though more than 2k chars are being used inside the array element:
    
    <script type="text/javascript">
    var script = new Array();
    script[0] = '1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd';
    script[1] = "2dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd";
    
    alert(script[0]);
    alert(script[1]);
    </script>
    
    Code (markup):
    I think the problem is the formatting of the string you are using. When your js is echoed you get something like this:
    quotations[$id] = "<table width="560" border="0"... etc, which won't work, because you need to mark beginning and end for the array element, use \' for that and \" for normal quotes inside html code you want to echo.
    Example [untested]:
    
    echo("quotations[$id] = \'<table width=\"560\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> ... end of the string...\';");
    
    PHP:
    Try not to use line breaks inside the array_element, because that could cause errors too...
     
    hogan_h, Dec 29, 2007 IP
    vlad230 likes this.
  3. vlad230

    vlad230 Active Member

    Messages:
    544
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    95
    #3
    No, unfortunately that isn't it.

    When I use my code and I examine the source code that the browser outputs, it looks like that:

    
    <SCRIPT type="text/javascript">
    var quotations = new Array()
     quotations[0] = "
    <table width="560" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="206">
    	<a href="http://example.com"><img src="http://example.com/img/banner.jpg" border="0" alt="bannerrr" /></a>
    </td>
    <td width="354">
    	<p><a href="http://example.com" class="link">text link</a><br />
    	(01.01.2008)<br /><br />
    	message bla bla bla....<br />
    	<b>img vid</b> 
    	</p>
    </td>
    </tr>
    </table>
    
    ";quotations[1] = ......................"
    
    Code (markup):
    And it look OK to me. But it doesn't work....

    When I used your code:

    
    <SCRIPT type="text/javascript">
    var quotations = new Array()
     quotations[0] = \'
    <table width="560" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="206">
    	<a href="http://example.com"><img src="http://example.com/img/banner.jpg" border="0" alt="bannerrr" /></a>
    </td>
    <td width="354">
    	<p><a href="http://example.com" class="link">text link</a><br />
    	(01.01.2008)<br /><br />
    	message bla bla bla....<br />
    	<b>img vid</b> 
    	</p>
    </td>
    </tr>
    </table>
    
    /';quotations[1] = ......................"
    
    Code (markup):
    That \' wasn't ok, clearly, so I changed it to ' but it still doesn't work...Javascript isn't cool :p


    EDIT: I forgot... if I use <br /> in a short array it works great.
     
    vlad230, Dec 29, 2007 IP
  4. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    AFAIK JS doesn't support multi-line strings.
     
    MMJ, Dec 29, 2007 IP
    vlad230 likes this.
  5. vlad230

    vlad230 Active Member

    Messages:
    544
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    95
    #5
    I just discovered that if I write all the code in one line, my old code works perfectly :) Javascript is strange:(

    EDIT:
    You're absolutely right ;)
    If you just have been here a few hours back :D

    Also I someone could help me out on this one:
    How could I make the display function to display the array from the last "quotation" to the first?

    Example:
    quotation[0] = '.....aA..'
    quotation[1] = '...sadasd....'
    quotation[2] = '...asdasd....'
    quotation[3] = '....asdasd...'
    quotation[4] = '...dsad....'
    quotation[5] = '...asdas....'

    and display them starting with quotation[5] ?

    
    function display()
    {
    a=Math.floor(Math.random()*quotations.length)
    document.getElementById('quotation').innerHTML=quotations[a]
    setTimeout("display()",5000)
    }
    
    PHP:
    I guess I need something modifie here:

    a=Math.floor(Math.random()*quotations.length)

    BTW, REPs sent
     
    vlad230, Dec 29, 2007 IP
    hogan_h likes this.
  6. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I would suggest following:
    After you have created in php complete quotations array, output inside the js script block new global variable, for example:
    var current_counter = quotations.length;
    Code (markup):
    then try this modified display() function:
    
    function display()
    {
    	if (current_counter < 0) current_counter = quotations.length;
    	
    	document.getElementById('quotation').innerHTML=quotations[current_counter];
    	setTimeout("display()",5000);
    	
    	current_counter--;
    }
    
    Code (markup):

    Warning:
    I didn't test the code, try it and see for yourself, consider it as just a hint for possible solution ;)
     
    hogan_h, Dec 29, 2007 IP
  7. vlad230

    vlad230 Active Member

    Messages:
    544
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    95
    #7
    Thanks a lot it works ok but I made a few minor modification to it like:
    var current_counter = quotations.length-1;
    Code (markup):
    	if (current_counter < 0) current_counter = quotations.length-1;
    Code (markup):
    Because the quotations start from 0 ;)

    Thanks a lot again!

    Vlad
     
    vlad230, Dec 30, 2007 IP
  8. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Yep, that's correct, i overlooked it, like i said, just wrote the code from top of my head, without testing ;)
     
    hogan_h, Dec 30, 2007 IP