...

View Full Version : How do I iterate this so I get a list?



enoxh
05-16-2012, 05:10 AM
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.




<?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>

Wojjie
05-16-2012, 06:05 AM
This already iterates through the data, with everything in this while loop being run for every item in the result:

// 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!

enoxh
05-16-2012, 06:19 AM
Thanks so much for responding. I had the same thought but no matter where I put
<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?

Wojjie
05-16-2012, 06:27 AM
Because the HTML is not PHP code, you need to echo it:

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:

?>
<a href="https://twitter.com/<?php echo $row(text)?>" class="twitter-follow-button" data-show-count="true" data-lang="en">Follow @ </a>
<?php

enoxh
05-16-2012, 06:53 AM
The first example throws this error. Parse error: syntax error, unexpected '/' in C:\xampp\htdocs\twexa\index.php on line 49




<?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?




<?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
05-16-2012, 09:27 AM
Thanks for your help I finally figured it out!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum