Hi Guys wanna ask some assistance/ help

I'm making something like notification with jquery and php and its working fine now the only problem that i encounter is on the notification its only show one data repeatedly its not showing the other data that supposed to be showed

Here's my script on showing the notification

<!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=UTF-8" />

<link href="jquery.notice.css" type="text/css" media="screen" rel="stylesheet" />


     <?php

    date_default_timezone_set('Asia/Manila');
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';

    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die 

    ('Error connecting to mysql');

    $dbname = 'newworld';
    mysql_select_db($dbname);
        //get the function


            $page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
            $limit = 10;
            $startpoint = ($page * $limit) - $limit;


            //to make pagination
            $statement = "`advance` where `active` = 1 ";
            $date=date('Y-m-d '); // Current Date
              $query = mysql_query("SELECT * FROM advance where Guest_type='VIP' AND SDate= '" . $date . "'  ORDER BY Id DESC LIMIT {$startpoint} , {$limit}");

                while ($rows = mysql_fetch_assoc($query))
                {
            if($rows['A1_Time']!=='00:00:00' || $rows['A2_Time']!=='00:00:00')
            {
              $a1=$rows['A1_Time']; // time of arrival to a designated 1ST AREA
              $a2=$rows['A2_Time']; // time of arrival to a designated 2ND AREA
              $name=$rows['Name'];



              echo"<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js' type='text/javascript'></script>";
             echo "<script src='jquery.notice.js' type='text/javascript'></script>";
              echo"<script type='text/javascript'> ";

             echo"var name='$name '; ";




              echo "$(document).ready(function()";
               echo"{";

                echo"jQuery.noticeAdd({text: ' Name is '+ name
                ,stay: true}); ";


        echo"});";
    echo"</script>";


            }

            else
            {

            }

            ?>


        <?php    
                }
            ?>



</body>
</html>

In my query it supposed to display

Name is Kurosaki Ichigo

Name is Guest 1

but its only displaying

Name is Kurosaki Ichigo

Name is Kurosaki Ichigo

two times

i'll also include a sceenshot to show you what i mean

Recommended Answers

All 5 Replies

Consider changing the code for your <script> tags from php echoes to html. Also, your script includes should be outside of the while statement. Else, they're being included every time the cycle iterates, like this:

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js' type='text/javascript'></script>
<script src='jquery.notice.js' type='text/javascript'></script>
<?php
// ... rest of the code goes here
while ($rows = mysql_fetch_assoc($query))
{
    if($rows['A1_Time']!=='00:00:00' || $rows['A2_Time']!=='00:00:00')
    {
              $a1=$rows['A1_Time']; // time of arrival to a designated 1ST AREA
              $a2=$rows['A2_Time']; // time of arrival to a designated 2ND AREA
              $name=$rows['Name'];
    ?>
    <script type='text/javascript'>
        var name='$name ';
        $(document).ready(function(){
            jQuery.noticeAdd({
                text: ' Name is '+ name,
                stay: true});
        });
    </script>
    <?php 
    }
}
// ... rest of code
?>

That way your code is easier to evaluate and debug.

Besides the above code, consider creating an array or a json object to store the notifications in the notify system. That way the $(document).ready() function is not called in every cycle of the while loop:

<script type="text/javascript">
    var notifications = {};
<?php
// while, if, etc
{
?>
    notifications.push({text: "the name is <?php echo $name; ?>", stay: true});
<?php
}
?>
    $(document).ready(function(){
        for(notif in notifications){
            jQuery.noticeAdd({
                text: notif.text,
                stay: notif.stay
            });
        }
    });
</script>

or something like that

Hi thanks i'll try what you've told me and be back when i have another question or something not making any sense with what im doing..

Hi i've tried what you told on on the first post and separated my js and php and it came up with a better and understandble script but im having some issue regarding your second post. I cant get the head and tails out of it can you elaborate it clearly..

by the way here's my current code

<!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=UTF-8" />

<link href="jquery.notice.css" type="text/css" media="screen" rel="stylesheet" />
  <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js' type='text/javascript'></script>
            <script src='jquery.notice.js' type='text/javascript'></script>


     <?php

    date_default_timezone_set('Asia/Manila');
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';

    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die 

    ('Error connecting to mysql');

    $dbname = 'newworld';
    mysql_select_db($dbname);
        //get the function



               $date=date('Y-m-d ');
              $query = mysql_query("SELECT * FROM advance where Guest_type='VIP' AND SDate= '" . $date . "' ");

                while ($rows = mysql_fetch_assoc($query))
                  {
                 if($rows['A1_Time']!=='00:00:00' || $rows['A2_Time']!=='00:00:00')
                       {
                       $a1=$rows['A1_Time']; // time of arrival to a designated 1ST AREA
                       $a2=$rows['A2_Time']; // time of arrival to a designated 2ND AREA
                       $name=$rows['Name'];
                   ?>


               <script type='text/javascript'>

               var name='<?php echo $name ?>';

               $(document).ready(function(){
               jQuery.noticeAdd({
               text: ' Name is '+ name,
               stay: true});
               });
               </script>

             <?php
             }
             }

             ?>



</body>
</html>

just the same still just dispalying 1 Name two times..

thanks

Sure thing. Here's the explanation for the code:

<!-- open the script tag before the while loop. This way
  -- you don't overload your webpage with unnecessary
  -- script tags -->
<script type="text/javascript">
var notifications = {}; // create an empty dictionary to store the database information
                        // you get from the server request
<?php
while ($rows = mysql_fetch_assoc($query)){
    if($rows['A1_Time']!=='00:00:00' || $rows['A2_Time']!=='00:00:00'){
        $a1=$rows['A1_Time']; // time of arrival to a designated 1ST AREA
        $a2=$rows['A2_Time']; // time of arrival to a designated 2ND AREA
        $name=$rows['Name'];
?>
    // add a new item at the end of the javascript dictionary (json object)
    // with the data from the database search
    notifications.push({
        text: "the name is <?php echo $name; ?>", //text key
        stay: true}); //stay key
<?php
    } /*End if*/
} /*End while*/
?>
    // open the $(document).ready() function after the loop
    // to avoid declaring it every time the loop runs
    $(document).ready(function(){
        // iterate (explore) through the elements of the dictionary we created earlier
        // for every item in the dictionary, create a variable called 'notif'
        for(notif in notifications){
            // add notice
            jQuery.noticeAdd({
                text: notif.text, //text key
                stay: notif.stay //stay key
            });
        }
    });
</script>
<!-- close the script tag at the end of the page, outside of any loops -->
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.