Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    42
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Read from table and display in center of page?

    Hello. I have searched the forums and have not found the exact thing i am trying to do.

    I need to read a line of text from the database, display it in the middle of the page, wait "x" number of seconds, then read another line and replace the text in the center of the page with the new line of text.

    The only issue I am having trouble with, is the posting of the text into the field in the middle of the screen.

    Here is some code so far..:

    Code:
    $queryname = "SELECT tsid FROM text_save WHERE myprog='$textname'";
    $resultname = mysql_query($queryname) or exit(mysql_error());		
    if($rowname = mysql_fetch_array($resultname, MYSQL_ASSOC)) {  
    $_SESSION['textid'] = $rowname['tsid'];
    }
    									
    $tsid = $_SESSION['textid']-1;
    ?>
    									
    <span id="textline">&nbsp;</span>
    																	
    									
    <?php
    $query = "SELECT * FROM text_lines WHERE tid='$tsid'";
    $result = mysql_query($query) or exit(mysql_error());
    $timepause = 6;
    									
    									
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {  
    									
    $textline = $row['text'];
    ?>
    <script>
    document.getElementById("textline").innerHTML = $textline;
    </script>
    <?php
    											
     }	} ?>
    I read from the database just fine. But how do i get it to display into the :

    Code:
    <span id="textline">&nbsp;</span>
    ?

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,568
    Thanks
    23
    Thanked 643 Times in 642 Posts
    It's the HTML/CSS that centers this:
    <div style="text-align:center;"><span id="textline"></span></div>
    You don't need the &nbsp; in the span.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You don't need the extra tag.
    Code:
    <div id="textline" style="text-align:center;"></div>
    Of course, best practice is to separate style from content.

    CSS:
    Code:
    #textline { text-align: center; }
    HTML:
    Code:
    <div id="textline"></div>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    And you don't actually need JS. Just move the div tag after $textline is set and then echo $textline inside the div tag.
    PHP Code:
    <?php

    $queryname 
    "SELECT tsid FROM text_save WHERE myprog='$textname'";
    $resultname mysql_query($queryname) or exit(mysql_error());        
    if(
    $rowname mysql_fetch_array($resultnameMYSQL_ASSOC)) {  
    $_SESSION['textid'] = $rowname['tsid'];
    }
                                        
    $tsid $_SESSION['textid']-1;

    $query "SELECT * FROM text_lines WHERE tid='$tsid'";
    $result mysql_query($query) or exit(mysql_error());
    $timepause 6;
                                        
                                        
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)) {  
                                        
    $textline $row['text'];
    }
    ?>
    <div id="textline"><?php echo $texline ?></div>
    Last edited by glenngv; 07-30-2014 at 11:44 PM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,568
    Thanks
    23
    Thanked 643 Times in 642 Posts
    Looks like what I said in first post.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #6
    New Coder
    Join Date
    Jul 2009
    Posts
    42
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ok, perhaps you all misunderstood something there. You all answered something that i did not ask. I need to place text in that element. I need to read another line from the database.. and replace the old text with the new text. Without refreshing the page or reloading the page. You cannot do this with html or php. Thusly, I think the question was completely misunderstood. There is other things on the page going on. The ONLY thing that needs updating with the new query form the database, is that one single element, without disturbing the rest of the page.

    I asked nothing about how to center the text. I asked for help on placing the variable into the element, time and again. I do not need the rest of the page disturbed, only that one single element to be replacing that text with each new line.

    Does this make it clearer? Thanks
    Last edited by iceregent; 07-31-2014 at 12:37 AM. Reason: mispelling

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You need AJAX to do this then. You need to initially display the first line and then after the page has loaded you repeatedly send an AJAX request to fetch the next line and then update the text.
    Last edited by glenngv; 07-31-2014 at 12:47 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #8
    New Coder
    Join Date
    Jul 2009
    Posts
    42
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I have no idea what ajax is. But I do know that one time before for a school project a year or so back I had to do such a thing with javascript. It was a clock, that only updated the digits in the clock, and did not use ajax, but I cant find that old project on my computer.

  • #9
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,568
    Thanks
    23
    Thanked 643 Times in 642 Posts
    You can learn about ajax here AJAX Example to start and the pages that come after. If you can obtain all of the lines and place them into an array, you could send that back and only have to use ajax once.

    OR knowing you need to do this make an array in javascript and use that to replace the lines when needed. No ajax needed at all.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #10
    New Coder
    Join Date
    Jul 2009
    Posts
    42
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ok.. so none of these "solutions" deals with my original problem/question at all. I need to place and replace a line of text in a page element, a <span> element would suffice. Seems to be there is confusion about what I am asking about. I do not need to know how to get info from my database. I do not need to know how to center the text. I do not need to know how to echo text. I need to know how to use javascript to replace text in a dom object on the page.

  • #11
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,568
    Thanks
    23
    Thanked 643 Times in 642 Posts
    <span id="textline">some span text</span>

    <script>
    document.getElementById('textline').innerHTML = 'Not the same text';
    </script>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #12
    New Coder
    Join Date
    Jul 2009
    Posts
    42
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I put that same thing in my code, and it doesn't work it seems..

    PHP Code:
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {  
                                        
    $textline = $row['text'];
    ?>
    <script>
    document.getElementById("textline").innerHTML = $textline;
    </script>
    <?php
                                                
     
    }

  • #13
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,568
    Thanks
    23
    Thanked 643 Times in 642 Posts
    You can not match PHP and JS that way. PHP is on the server, it must be accessed from the page or it was used to create the page. JS is on the page, therefore local to you.

    Here's a question. Do you get the $row['text'] while php is generating the page OR does something on the page determine what is retrived from the DB?
    Answer and we will proceed from there.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #14
    New Coder
    Join Date
    Jul 2009
    Posts
    42
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    Do you get the $row['text'] while php is generating the page OR does something on the page determine what is retrived from the DB?
    Answer and we will proceed from there.
    The answer is in the code i already posted.

    PHP Code:
    $query = "SELECT * FROM text_lines WHERE tid='$tsid'";
    $result = mysql_query($query) or exit(mysql_error());
    $timepause = 6;
                                        
                                        
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {  
                                        
    $textline = $row['text'];
    ?>
    <script>
    document.getElementById("textline").innerHTML = $textline;
    </script>
    <?php
                                                
     
    }
    Thusly, the $row['text'] is fetched in a loop as long as the criteria is met, in other words, as long as there is text to read.

    Now lets see if i can attempt to answer the next anticipated argument I guess.. I already know how to retrieve text from the database. I already know it works. simple : echo $row['text'] prints out all of the text in the database one line at a time, albeit all running beneath each other rather than in the <span> location.

    such as follows:

    PHP Code:
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {  
                                        
    $textline = $row['text'];
    echo $row['text'];
    ?>
    <script>
    document.getElementById("textline").innerHTML = $textline;
    </script>
    <?php
                                                
     
    }
    Please see the code where i entered the echo statement in. Its inside the while loop that reads the text from the database. So, yes, it does read and echo out each and every line i need it to read out. The only thing it does NOT do right now, is place it inside the <span> element.

  • #15
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    <script>
    document.getElementById("textline").innerHTML = "<?php echo $textline; ?>";
    </script>
    Note that if $textline contains newlines/carriage returns, you have to replace them in php with <br />.
    Last edited by glenngv; 08-01-2014 at 11:38 PM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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