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 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I iterate this so I get a list?

    Sorry I just realized this should have been in server side...

    I'm trying to create a list of twitter users with a follow button next to each user. The user names are being pulled from a mysql database.

    Here's the code I have so far. I can get a list of user names from my database, put it into an array and I can replace the user name in the first follow button. I need to sequentially replace the $row variable in the script at the bottom so the follow button points to the users account each time it iterates through the array?

    I'm stuck on how to iterate this so that I get a list of all the users in the array with a follow button next to each user name.

    Any help or ideas would be greatly appreciated!

    I'm stuck and on a deadline.

    Code:
    <?php
       // Connect to database server
      mysql_connect("localhost", "root", "") or die (mysql_error ());
     
     // Select database
     mysql_select_db("tewxadb") or die(mysql_error());
     
       // SQL query
       $strSQL = "SELECT * FROM users";
     
        // Execute the query (the recordset $rs contains the result)
       $rs = mysql_query($strSQL);
     
       // Loop the recordset $rs
      // Each row will be made into an array ($row) using mysql_fetch_array
      while($row = mysql_fetch_array($rs)) {
     
           // Write the value of the column username (which is now in the array $row)
       echo $row['username'] . "<br />";
     
       }
     
       // Close the database connection
       mysql_close();
     ?>
     
     
    <a href="https://twitter.com/" +$row(text) class="twitter-follow-button" data-show-count="true" data-lang="en">Follow </a>   
     
    <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
    Last edited by enoxh; 05-16-2012 at 05:24 AM. Reason: I messed up

  • #2
    Regular Coder
    Join Date
    Apr 2011
    Posts
    286
    Thanks
    2
    Thanked 39 Times in 39 Posts
    This already iterates through the data, with everything in this while loop being run for every item in the result:
    Code:
       // Loop the recordset $rs
      // Each row will be made into an array ($row) using mysql_fetch_array
      while($row = mysql_fetch_array($rs)) {
     
           // Write the value of the column username (which is now in the array $row)
       echo $row['username'] . "<br />";
     
       }
    Now you just have to move your "Follow" code into that while loop for it to work!

  • #3
    New to the CF scene
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks so much for responding. I had the same thought but no matter where I put
    Code:
     <a href="https://twitter.com/" +$row(text) class="twitter-follow-button" data-show-count="true" data-lang="en">Follow @ </a>
    I get a parse error?

    Do I have to use different syntax when I put A HREF into the while loop?
    Also do I have to include the javascript in the loop or can I just leave that outside?

  • #4
    Regular Coder
    Join Date
    Apr 2011
    Posts
    286
    Thanks
    2
    Thanked 39 Times in 39 Posts
    Because the HTML is not PHP code, you need to echo it:
    Code:
    echo "<a href=\"https://twitter.com/" . $row(text) ."\" class=\"twitter-follow-button\" data-show-count=\"true\" data-lang=\"en\">Follow @ </a>
    or go in and out of PHP tags:
    Code:
     ?>
    <a href="https://twitter.com/<?php echo $row(text)?>" class="twitter-follow-button" data-show-count="true" data-lang="en">Follow @ </a>
    <?php

  • #5
    New to the CF scene
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The first example throws this error. Parse error: syntax error, unexpected '/' in C:\xampp\htdocs\twexa\index.php on line 49

    Code:
    <?php
    	// Connect to database server
    	mysql_connect("localhost", "root", "") or die (mysql_error ());
    
    	// Select database
    	mysql_select_db("tewxadb") or die(mysql_error());
    
    	// SQL query
    	$strSQL = "SELECT * FROM users";
    
    	// Execute the query (the recordset $rs contains the result)
    	$rs = mysql_query($strSQL);
    	
    	// Loop the recordset $rs
    	// Each row will be made into an array ($row) using mysql_fetch_array
    	while($row = mysql_fetch_array($rs)) {
    	
    	
    
    	   // Write the value of the column FirstName (which is now in the array $row)
    	  echo $row['username'] . "<br />";
    
    echo "<a href=\"https://twitter.com/" . $row(text) ."\" class=\"twitter-follow-button\" data-show-count=\"true\" data-lang=\"en\">Follow @ </a>
    	  }
    
    	// Close the database connection
    	mysql_close();
    	?>
    
    <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
    The second example doesn't throw an error but only the name from the DB appears, there is no follow button visible?

    Code:
    <?php
    	// Connect to database server
    	mysql_connect("localhost", "root", "") or die (mysql_error ());
    
    	// Select database
    	mysql_select_db("tewxadb") or die(mysql_error());
    
    	// SQL query
    	$strSQL = "SELECT * FROM users";
    
    	// Execute the query (the recordset $rs contains the result)
    	$rs = mysql_query($strSQL);
    	
    	// Loop the recordset $rs
    	// Each row will be made into an array ($row) using mysql_fetch_array
    	while($row = mysql_fetch_array($rs)) {
    	
    	
    
    	   // Write the value of the column FirstName (which is now in the array $row)
    	  echo $row['username'] . "<br />";
    
     ?>
    <a href="https://twitter.com/<?php echo $row(text)?>" class="twitter-follow-button" data-show-count="true" data-lang="en">Follow @ </a> 
    
    
    <?php
    
    	  }
    
    	// Close the database connection
    	mysql_close();
    	?>
    
    
    <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
    Thank you so much for taking the time, I feel sooo close but I've been trying to solve this for 5 hours and my brain is blurry at this point...

  • #6
    New to the CF scene
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Solved

    Thanks for your help I finally figured it out!


  •  

    Tags for this Thread

    Posting Permissions

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