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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Incremental PHP Value?

    Hey guys!

    I've been trying to work my head around an issue for quite a while, but haven't been able to figure this out myself.
    Keep in mind i'm new to php / web coding in general. (:

    Pretty much there is 3 'cards' on my site.
    The three cards display blocks of information that is stored on a database.

    What i'm trying to create is basically a button that increases the capnum value by 1 on all 3 cards.
    So the first 1 turns to 2, 2 turns to 3, 3 turns to 4 and so forth.
    Also, I don't want the page to be 'refreshed' when the button is pressed.


    Currently this is my code:

    PHP Code:
    $capnum=1;
    mysql_connect($hostname$username$password$dbname);
    mysql_select_db("db_thght")or die("cannot select DB");
    $sql "SELECT * FROM `thoughtstat` WHERE ID = $capnum LIMIT 0, 30 ";
    $result=mysql_query($sql);
    while(
    $rows=mysql_fetch_array($result)){
    echo 
    $rows['Title'];} 
    PHP Code:
    $capnum=2;
    mysql_connect($hostname$username$password$dbname);
    mysql_select_db("db_thght")or die("cannot select DB");
    $sql "SELECT * FROM `thoughtstat` WHERE ID = $capnum LIMIT 0, 30 ";
    $result=mysql_query($sql);
    while(
    $rows=mysql_fetch_array($result)){
    echo 
    $rows['Title'];} 
    PHP Code:
    $capnum=3;
    mysql_connect($hostname$username$password$dbname);
    mysql_select_db("db_thght")or die("cannot select DB");
    $sql "SELECT * FROM `thoughtstat` WHERE ID = $capnum LIMIT 0, 30 ";
    $result=mysql_query($sql);
    while(
    $rows=mysql_fetch_array($result)){
    echo 
    $rows['Title'];} 
    Any help would be appreciated, thanks!

  • #2
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post
    I'm not sure if I've understood you correctly but if I have then you'll more likely be using javascript to update button values. PHP is server side so a page reload would be required unless you use AJAX. Also when dealing with such confined scopes you could consider using PHP's switch functionality.

    Sorry I'm in work right now so I don't have time to code examples - will pop back on later and help if you're still having issues.

  • #3
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post
    PHP's a server side language - updating front-end data such as button text and values is best accomplished with javascript - consider jQuery for this.

    Also I see code blocks with only a variable different in each - is this just for ease of display or is this how the code appears exactly? If the latter, consider breaking all the db connection data and repetitive code into a separate function.

    Also, as ever I'd strongly recommend using PDO to populate the database

    Hope this helps - I'm in work right now so don't have time for example code but I'll have more time later to help if you're still stuck!

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,166
    Thanks
    23
    Thanked 600 Times in 599 Posts
    To add to what grahamd1984 said:
    You do not need to query your DB three times. You already have that information. Move the info on card two to card one and card three to card two. Then populate card three with the new query info.
    Here's a good place to learn ajax http://www.w3schools.com/ajax/ajax_intro.asp read it all.

    Here's an example of what I explained above minus the get_info() function which is ajax.
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <style type="text/css">
    #card1, #card2, #card3{
    	width: 300px;
    	height: 100px;
    	background-color: gray;
    	margin: 10px;
    	border: 1px solid black;
    }
    </style>
    </head>
    
    <body>
    <div id="card1">This is info on card one</div>
    <div id="card2">This is info on card two</div>
    <div id="card3">This is info on card three</div>
    <button onclick="firedrill();">The Button</button>
    
    <script type="text/javascript">
    function firedrill(){
    	var A = document.getElementById("card1").innerHTML;
    	var B = document.getElementById("card2").innerHTML;
    	var C = document.getElementById("card3").innerHTML;
    	//var D = get_info(4);
    
    	document.getElementById("card1").innerHTML = B;
    	document.getElementById("card2").innerHTML = C;
    	document.getElementById("card3").innerHTML = "A substitute string for the ajax return variable";
    }
    </script>
    </body>
    </html>
    We can talk about your PHP when you get this mastered.
    Evolution - The non-random survival of random variants.

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

  • #5
    New to the CF scene
    Join Date
    May 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by grahamd1984 View Post
    PHP's a server side language - updating front-end data such as button text and values is best accomplished with javascript - consider jQuery for this.

    Also I see code blocks with only a variable different in each - is this just for ease of display or is this how the code appears exactly? If the latter, consider breaking all the db connection data and repetitive code into a separate function.

    Also, as ever I'd strongly recommend using PDO to populate the database

    Hope this helps - I'm in work right now so don't have time for example code but I'll have more time later to help if you're still stuck!
    It's exactly how the code appears n_n; I know it's bad coding practice, but i'm just trying to get the functionality of the site working for now.
    Also, it's the first time i've done something with a database, so I wasn't even aware of PDO being a 'lighter' version for PHP > Database. I shall have a sus of that later, thanks! (:


    Quote Originally Posted by sunfighter View Post
    To add to what grahamd1984 said:
    You do not need to query your DB three times. You already have that information. Move the info on card two to card one and card three to card two. Then populate card three with the new query info.
    Here's a good place to learn ajax http://www.w3schools.com/ajax/ajax_intro.asp read it all.

    Here's an example of what I explained above minus the get_info() function which is ajax.
    Code:
    <body>
    <div id="card1">This is info on card one</div>
    <div id="card2">This is info on card two</div>
    <div id="card3">This is info on card three</div>
    <button onclick="firedrill();">The Button</button>
    
    <script type="text/javascript">
    function firedrill(){
    	var A = document.getElementById("card1").innerHTML;
    	var B = document.getElementById("card2").innerHTML;
    	var C = document.getElementById("card3").innerHTML;
    	//var D = get_info(4);
    
    	document.getElementById("card1").innerHTML = B;
    	document.getElementById("card2").innerHTML = C;
    	document.getElementById("card3").innerHTML = "A substitute string for the ajax return variable";
    }
    </script>
    We can talk about your PHP when you get this mastered.
    Ah I see, I've removed the unnecessary query to the database multiple times, thanks for the insight.

    Code:
    <div id="card1">This is info on card one</div>
    <div id="card2">This is info on card two</div>
    <div id="card3">This is info on card three</div>
    My 'Card' set up is a bit different to the one you've mentioned however. The information that's displayed on the card is taken from a database; not coded into the HTML.

    PHP Code:
    <div class="card_head">
    <!-- Card title -->
    <? 
    $capnum
    =1;
    mysql_connect($hostname$username$password$dbname);
    mysql_select_db("db_thght")or die("cannot select DB");
    $sql "SELECT * FROM `thoughtstat` WHERE ID = $capnum LIMIT 0, 30 ";
    $result=mysql_query($sql);
    while(
    $rows=mysql_fetch_array($result)){
    echo 
    $rows['Title'];}
    ?>
    </div>
    <div class="card_body">
    <!-- Card Content -->
    <? 
    $capnum
    =1;
    $result=mysql_query($sql);
    while(
    $rows=mysql_fetch_array($result)){
    echo 
    $rows['Thought'];}
    ?>
    </div>
    <div class="card_head">
    <!-- Card Title -->
    <?
    $capnum
    =2;
    $sql "SELECT * FROM `thoughtstat` WHERE ID = $capnum LIMIT 0, 30 ";
    $result=mysql_query($sql);
    while(
    $rows=mysql_fetch_array($result)){
    echo 
    $rows['Title'];}
    ?>
    </div>
    <div class="card_body">
    <!-- Card Content -->
    <? 
    $capnum
    =2;
    $result=mysql_query($sql);
    while(
    $rows=mysql_fetch_array($result)){
    echo 
    $rows['Thought'];}
    ?>
    </div>
    There's a 3rd card as well, but you guys get the picture;
    Pretty much i'm trying to create 2 buttons:
    1 would increase the capnum by 1 on each,
    and the other button that decreases the capnum by 1 on each.

    Would AJAX's get_info() be suffice in this situation? Or can I use jQuery to change increase/decrease the capnum value?

    Thanks again guys;

  • #6
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,166
    Thanks
    23
    Thanked 600 Times in 599 Posts
    Quote Originally Posted by Spade View Post
    My 'Card' set up is a bit different to the one you've mentioned however. The information that's displayed on the card is taken from a database; not coded into the HTML.
    Yes I know that. I just don't have access to that DB so I had to hard code something in so you could see it work.

    =========
    Some things you need to know about the last bit of PHP code you posted:

    Do not use <? for the start tag, use <?php. It is not available on most servers and if you want to be able to move your site don't use it. Also <? can be interpreted as something different (don't remember what) then PHP which is why it was removed.

    Next separate file for DB connection:
    As grahamd1984 said there is a new way to interact with the DB; PDO and mysqli. I use the latter. I say that so you will understand the following ex..
    Put the connection code in a seperate file and call it at the start of your PHP file. That way you don't have to code it every time you need to use it.
    I place mine in a folder named 'inc'.This is the file:
    PHP Code:
    <?php
    $hostname 
    "put yours here";
    $username="put yours here";
    $password="put yours here";
    $dbname="put yours here";

    $conn = new mysqli($hostname,$username,$password,$dbname);
    if(
    mysqli_connect_errno()) {
      echo 
    "Connection Failed: " mysqli_connect_errno();
      exit();
    }
    ?>
    I call it in the file that uses it like this:
    PHP Code:
    require ('./inc/DB_connectsqli.php'); 
    Next querying everything from the table:
    PHP Code:
    SELECT FROM.... 
    Is OK if you only have two columns in the table.
    Else use
    PHP Code:
    SELECT TitleThought FROM 
    Column names are case sensitive. So Title is not the same as title. It is recommended that you use low case all the time just like you should never use a space(use _).

    Next querying twice for the same information; use this:
    PHP Code:
        while($rows mysqli_fetch_row($result)){
            
    $title.$capnum $rows['Title'];
            
    $thought.$capnum $rows['Thought'];
        } 
    To save on repeating your query three time I have converted to a for loop:
    PHP Code:
    <?php
    require ('./inc/DB_connectsqli.php');

    for(
    $capnum 1$capnum <= 3$capnum++){
        
    $sql "SELECT * FROM `thoughtstat` WHERE ID = $capnum LIMIT 0, 30 ";
        
    $result mysqli_query($conn$query);
        while(
    $rows mysqli_fetch_row($result)){
            
    $title.$capnum $rows['Title'];
            
    $thought.$capnum $rows['Thought'];
        }
    }
    ?>
    And now we come to the last - keep PHP separate from the HTML as much as possible. Easier to trouble shoot.
    The above loop makes six variables:
    $title1, $thought1, $title2, $thought2, $title3, $thought3
    In your HTML you do this:
    Code:
    <div class="card_head">
    <?php echo $title1; ?>
    </div>
    
    <div class="card_body">
    <?php echo $thought1; ?>
    </div>
    
    <div class="card_head">
    <?php echo $title2; ?>
    </div>
    
    <div class="card_body">
    <?php echo $thought2; ?>
    </div>
    
    <div class="card_head">
    <?php echo $title3; ?>
    </div>
    
    <div class="card_body">
    <?php echo $thought3; ?>
    </div>
    For your last question
    Would AJAX's get_info() be suffice in this situation?
    get_info() is a function that you will write. In that function you will use ajax to get the next card info and pass it back using 'return'. Once you study ajax you will understand. And the 4 I passed is not static, it should be a variable that tell the DB what you need and yes it can also be used to go backwards.
    BUT right now start with a single piece of info and figure out the increase first.
    Last edited by sunfighter; 01-04-2014 at 05:03 PM.
    Evolution - The non-random survival of random variants.

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


  •  

    Posting Permissions

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