Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-16-2012, 05:10 AM   PM User | #1
enoxh
New to the CF scene

 
Join Date: May 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
enoxh is an unknown quantity at this point
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
enoxh is offline   Reply With Quote
Old 05-16-2012, 06:05 AM   PM User | #2
Wojjie
Regular Coder

 
Join Date: Apr 2011
Posts: 286
Thanks: 2
Thanked 39 Times in 39 Posts
Wojjie is on a distinguished road
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!
Wojjie is offline   Reply With Quote
Old 05-16-2012, 06:19 AM   PM User | #3
enoxh
New to the CF scene

 
Join Date: May 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
enoxh is an unknown quantity at this point
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?
enoxh is offline   Reply With Quote
Old 05-16-2012, 06:27 AM   PM User | #4
Wojjie
Regular Coder

 
Join Date: Apr 2011
Posts: 286
Thanks: 2
Thanked 39 Times in 39 Posts
Wojjie is on a distinguished road
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
Wojjie is offline   Reply With Quote
Old 05-16-2012, 06:53 AM   PM User | #5
enoxh
New to the CF scene

 
Join Date: May 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
enoxh is an unknown quantity at this point
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...
enoxh is offline   Reply With Quote
Old 05-16-2012, 09:27 AM   PM User | #6
enoxh
New to the CF scene

 
Join Date: May 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
enoxh is an unknown quantity at this point
Solved

Thanks for your help I finally figured it out!
enoxh is offline   Reply With Quote
Reply

Bookmarks

Tags
java, mysql, php, twitter

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:39 PM.


Advertisement
Log in to turn off these ads.