943,169 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 70
  • PHP RSS
Sep 3rd, 2010
0

Simple array technique Q

Expand Post »
Hey
Im getting there with PHP but one thing that does my head in is arrays.
Im trying to get all the content from the db and echo it..

For exmaple, in the db it reads under the 'names' column...
matt
harry
dave
katie
trisha
paul
tracy
brian
martin
sam

I want to be able to echo this on a page, each on a new line.

But, if a new name is added to the database, i want it to automatically display the new one etc

Probably really easy for you lot and maybe ive over complicated the explanation.

Hope you guys can do it.
Also, there is £5 in it via paypal for whoever comes up with a working script.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
Clarkeez is offline Offline
25 posts
since Sep 2010
Sep 4th, 2010
0
Re: Simple array technique Q
php Syntax (Toggle Plain Text)
  1. <?php
  2. mysql_connect("locahost","username","password") or die(mysql_error());
  3.  
  4. //change "dbname" to the name of your actual database
  5. mysql_select_db("dbname") or die(mysql_error());
  6.  
  7. //change "TABLENAME" to the name of the actual table you have
  8. $result=mysql_query("SELECT `names` FROM TABLENAME`") or die( mysql_error() );
  9. while( $row=mysql_fetch_assoc($result) )
  10. {
  11. echo '<div>' . $row['names'] . '</div>';
  12. }
  13. mysql_close();
  14. ?>
Reputation Points: 116
Solved Threads: 243
Veteran Poster
hielo is offline Offline
1,123 posts
since Dec 2007
Sep 4th, 2010
0

This might help you

Here is how it's done, first we perform all necessary database connections, the we query the database, after obtaining the result-set we transform it to an array The and lastly we print it's content using a foreach loop structure.

php Syntax (Toggle Plain Text)
  1. <?php
  2. var $link; //SQl Link variable.
  3. var $query; //SQL query string.
  4. var $result; //Where the result set is going to be stored.
  5. var $array; //The result-set as an array
  6. var $table; //Table name
  7.  
  8. $link=mysql_connect($host,$username,$passsword) or die("SQL Server error");
  9. mysql_select_db($database,$link) or die("Database Error");
  10.  
  11. $query="SELECT names FROM $table "; //Build query string.
  12. $result=mysql_query($query,$link); //Get the result-set.
  13.  
  14. /* Transform the result-set to an array so we can loop through it's data */
  15. $array=mysql_fetch_array($result);
  16. foreach($array as $value)
  17. echo $value . "<br />";
  18. ?>

Regards, Triztian
Reputation Points: 10
Solved Threads: 4
Light Poster
Triztian is offline Offline
28 posts
since Sep 2009
Sep 4th, 2010
0
Re: Simple array technique Q
OkThanks it works!
1 more thing to get this to my needs.
How can I make it into a variable so i can use it on another page.

I tried this..
php Syntax (Toggle Plain Text)
  1. while( $row_hardware=mysql_fetch_assoc($pull_floats_hardware) )
  2. {
  3. $row_hardware2 = '' . $row_hardware['name'] . '<br/>';
  4. }
Then
php Syntax (Toggle Plain Text)
  1. echo $row_hardware2;
on another page but it only shows 1 row
Reputation Points: 10
Solved Threads: 1
Light Poster
Clarkeez is offline Offline
25 posts
since Sep 2010
Sep 4th, 2010
0
Re: Simple array technique Q
PHP Syntax (Toggle Plain Text)
  1. Here is how it's done... /* Transform the result-set to an array so we can loop through it's data */
You are simply printing all the columns of the first record. The goal if to print all the records from a column. Refer to my post.


Quote ...
How can I make it into a variable so i can use it on another page. on another page but it only shows 1 row
save your db result as an array:

To clarify, on what you have:
$row_hardware2 = '' . $row_hardware['name'] . '<br/>';

$row_harware2 is NOT an array. But if you did:
$row_hardware2[] = '' . $row_hardware['name'] . '<br/>';
then execute:
PHP Syntax (Toggle Plain Text)
  1. foreach($row_hardware2 as $index=>$v){
  2. echo $i .': ' . $v;
  3. }

If you really need it on a completely different page, then use a $_SESSION variable instead. Just make sure that at the beginning of your file you have:
<?php session_start(); then the body of your while loop should be
$_SESSION['row_hardware2'][] = '' . $row_hardware['name'] . '<br/>';
and your foreach:
PHP Syntax (Toggle Plain Text)
  1. foreach($_SESSION['row_hardware2'] as $index=>$v){
  2. echo $i .': ' . $v;
  3. }
Last edited by hielo; Sep 4th, 2010 at 1:42 pm.
Reputation Points: 116
Solved Threads: 243
Veteran Poster
hielo is offline Offline
1,123 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: What is wrong with this update?
Next Thread in PHP Forum Timeline: How to show link from mysql query.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC