SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with SELECT not working properly in JavaScript function

    Hi there:

    I cannot get the code below to work properly. It works fine on the first loop but the next times around "$querySQL = 'SELECT * FROM host_index'" returns the wrong value.

    Can somebody help me out?

    <?php
    require_once('connections/radiolightstream.php');

    mysql_select_db($database_radiolightstream, $radiolightstream);
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <SCRIPT LANGUAGE="JavaScript">

    <SCRIPT LANGUAGE="JavaScript">

    // ***** Modify the messages here ***** //
    var Message = new Array(3);
    Message[0]=
    "<?php
    $querySQL = 'SELECT * FROM host_index'; // get next host index
    $result = mysql_query($querySQL, $radiolightstream) or die(mysql_error());
    $result = mysql_query($querySQL, $radiolightstream) or die(mysql_error());
    $row_result = mysql_fetch_array ($result);
    echo $row_result['next_host'].'<p>';
    $host_id = $row_result['next_host'] + 1; // actual host id
    ?>";
    Message[1]=
    "<?php
    $querySQL = 'SELECT * FROM hosts WHERE approved=1 AND paid=1';
    $result = mysql_query($querySQL, $radiolightstream) or die(mysql_error());
    $num_hosts = mysql_numrows($result);
    mysql_data_seek($result, $row_result['next_host']) or die(mysql_error());
    $row_host_result = mysql_fetch_array ($result);
    echo $row_host_result[show_title].'<p>';
    ?>";
    Message[2]=
    '<?php
    $next_host = $host_id % $num_hosts; // update pointer
    $querySQL = "UPDATE host_index SET next_host=$next_host";
    $result = mysql_query($querySQL, $radiolightstream) or die(mysql_error());
    echo $querySQL."<p>";
    ?>';

    var nCount = 0;
    function rotateSpan1()
    {
    Span1.innerHTML=Message[nCount];
    nCount++;
    if(nCount>Message.length-1) nCount=0;
    setTimeout("rotateSpan1()",5000);
    }
    function showSpan1(obj)
    {
    Span1.innerHTML=obj
    }

    </SCRIPT>
    </head>

    <body>
    <div id=Span1></div>
    <SCRIPT LANGUAGE="JavaScript">
    rotateSpan1();
    </SCRIPT>
    </body>
    </html>

    Genia

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This sounds like more of a SQL/PHP problem, not a JS one, try the PHP forum. If you still think it's a JS issue, please post the rendered code that a browser would see.

  3. #3
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there:

    I cannot get the code below to work properly. It works fine on the first loop but the next times around "$querySQL = 'SELECT * FROM host_index'" returns the wrong value.

    Specifically, here is what happens...

    The first time 'SELECT next_host FROM host_index' is executed, it works properly. Let's say it is '0';

    The following command 'SELECT * FROM hosts WHERE approved=1 AND paid=1' also works properly. Then mysql_data_seek($result, 0]) or die(mysql_error()); works properly.

    Finally, "UPDATE host_index SET next_host=1" works properly. I verified this using phpMyAdmin and next_host in the table host_index is now set to '1'.

    The problem starts with subsequent loops in this function.

    The next time 'SELECT next_host FROM host_index' is executed, it does not work properly. It says it is '0' but it is really set to 'i' .

    This is the problem: the table value (set to '1') is correct but the SELECT command is not working properly because it returns '0' in all subsequent loops and there is no error message.

    Can somebody help me out?


    Code:



    PHP Code:

    <?php
    require_once('connections/radiolightstream.php');

    mysql_select_db($database_radiolightstream, $radiolightstream);
    ?>




    ,html]
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <SCRIPT LANGUAGE="JavaScript">

    // ***** Modify the messages here ***** //
    var Message = new Array(3);
    Message[0]=
    "
    PHP Code:

    <?php
    $querySQL = 'SELECT next_host FROM host_index'; // get next host index
    $result = mysql_query($querySQL, $radiolightstream) or die(mysql_error());
    $row_result = mysql_fetch_array ($result);
    echo $row_result['next_host'].'<p>';
    $host_id = $row_result['next_host'] + 1; // actual host id
    ?>



    ";
    Message[1]=
    "
    PHP Code:

    <?php
    $querySQL = 'SELECT * FROM hosts WHERE approved=1 AND paid=1';
    $result = mysql_query($querySQL, $radiolightstream) or die(mysql_error());
    $num_hosts = mysql_numrows($result);
    mysql_data_seek($result, $row_result['next_host']) or die(mysql_error());
    $row_host_result = mysql_fetch_array ($result);
    echo $row_host_result[show_title].'<p>';
    ?>,/php]";
    Message[2]=
    '[php]<?php
    $next_host = $host_id &#37; $num_hosts; // update pointer
    $querySQL = "UPDATE host_index SET next_host=$next_host";
    $result = mysql_query($querySQL, $radiolightstream) or die(mysql_error());
    echo $querySQL."<p>";
    ?>



    ';

    var nCount = 0;
    function rotateSpan1()
    {
    Span1.innerHTML=Message[nCount];
    nCount++;
    if(nCount>Message.length-1) nCount=0;
    setTimeout("rotateSpan1()",5000);
    }
    function showSpan1(obj)
    {
    Span1.innerHTML=obj
    }

    </SCRIPT>
    </head>

    <body>
    <div id=Span1></div>
    <SCRIPT LANGUAGE="JavaScript">
    rotateSpan1();
    </SCRIPT>
    </body>
    </html>
    [/html]


    Genia

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is not so interesting to see your SQL and PHP code, simply show the output of the HTML/JavaScript instead.

    Also, use the code tags to show the code.

  5. #5
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The output is

    Loop 1:

    0 (correct)
    Tazz and Paula Show (correct)
    1 (correct - and database is updated correctly)

    Loop 2:

    0 (incorrect - should be 1 - however, the database has the correct value and SELECT is not working properly)
    Tazz and Paula Show (read correctly but wrong value because the SELECT above did not work properly)
    1 (wrong value because the SELECT above did not work properly)

    This will repeat for all subsequent loops.

    THE CORRECT OUTPUT SHOULD BE...

    Loop 1:

    0
    Tazz and Paula Show
    1

    Loop 2:

    1
    Tisha Rivas
    2

    Loop 3:

    2
    Adam Atman
    0

    Loop 4:

    0
    Tazz and Paula Show
    1

    and so on...

    The problem is with SELECT not working properly after the first loop.

    Genia

  6. #6
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I meant with output is the HTML code from your code above... In Firefox --> /View/Page Source

  7. #7
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>

    <script language="JavaScript">
    <!--

    function SymError()
    {
    return true;
    }

    window.onerror = SymError;

    var SymRealWinOpen = window.open;

    function SymWinOpen(url, name, attributes)
    {
    return (new Object());
    }

    window.open = SymWinOpen;

    //-->
    </script>

    <SCRIPT LANGUAGE="JavaScript">

    // ***** Modify the messages here ***** //
    var Message = new Array(3);
    Message[0]=
    "2<p>";
    Message[1]=
    "Adam Atman<p>";
    Message[2]=
    'UPDATE host_index SET next_host=0<p>';

    var nCount = 0;
    function rotateSpan1()
    {
    Span1.innerHTML=Message[nCount];
    nCount++;
    if(nCount>Message.length-1) nCount=0;
    setTimeout("rotateSpan1()",5000);
    }
    function showSpan1(obj)
    {
    Span1.innerHTML=obj;
    }

    </SCRIPT>
    </head>

    <body>
    <div id=Span1></div>
    <SCRIPT LANGUAGE="JavaScript">
    rotateSpan1();
    </SCRIPT>
    </body>
    </html>

    <script language="JavaScript">
    <!--
    var SymRealOnLoad;
    var SymRealOnUnload;

    function SymOnUnload()
    {
    window.open = SymWinOpen;
    if(SymRealOnUnload != null)
    SymRealOnUnload();
    }

    function SymOnLoad()
    {
    if(SymRealOnLoad != null)
    SymRealOnLoad();
    window.open = SymRealWinOpen;
    SymRealOnUnload = window.onunload;
    window.onunload = SymOnUnload;
    }

    SymRealOnLoad = window.onload;
    window.onload = SymOnLoad;

    //-->
    </script>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •