chrometweaks.org

SingleHop Dedicated Web iPage hosting review?

Click Here To View All Answers...


First of all SingleHop Dedicated Web iPage hosting review? Many thanks for any response. Second question.. Hi,.

I have searched all over the web for an answer and I must not be using the right search terms because I can't come up with anything. I am basically trying to replicate the automatic looping functionality you get with a statement like this:.

Where ($value = tep_db_fetch_array($arr)) {.

//code.

}.

OR.

Where ($value = mysql_fetch_assoc($arr)) {.

//code.

}.

I need to create that functionality using an array (and presumably foreach) instead of the mysql_fetch_assoc function. The reason for this is above this section I will be grabbing some values out of the database and turning that result into an array using the mysql_fetch_assoc function, but I will be getting two sets of values, creating two arrays, then combining them into one array to be processed by the loop. The most important feature I need is to be able to access the values by their keys, so for example using the above samples, in the code section I would need to make references to $value['products_id'] or $value['products_name']. Apparently the mysql_fetch_assoc function returns a special kind of array or something because the closest I have come is the following code:.

Foreach ($value as $k => $v) {.

Echo " TEST: $v";.

}.

The select statment that creates the array $value that I'm feeding into the foreach looks like this: "SELECT p.products_id, p.products_image, pd.products_name", and it returns six records. Instead of the foreach iterating through each of the six records, it iterates through each of the three fields so the echo statement above lists the values of those three fields for the first record..

How do I get it to loop through each of the records and let me access the fields by their key names? How do I get this to function just like the where ($array = mysql_fetch_assoc) statement?.

Thanks!.

This post has been edited by.

Luchysoft.

: 19 August 2004, 17:34..

Comments (128)

I would like to know the answer too. Anyone here know what is the answer. I'll do some poking around and get back to you if I find an answer. You should email the people at iPage as they probably could answer your iPage question..

Comment #1

JanZ:.

Thanks for the reply, but unfortunately the example you gave is what I already know how to do and referenced in the top portion of my original post. In line 19 of your code listing within the while condition, it sets $a_row equal to the result of mysql_fetch_row($result) and $result is just the query. This code is slightly different but has the same effect as where ($value = mysql_fetch_assoc($arr)) , which I listed in the top of my post..

My understanding of that line 19 and my sample in the top portion of the post is that the mysql_fetch_row and mysql_fetch_assoc both return an array representation of the recordset coming back from the query. Somehow by magic, php knows to iterate through the array as if it were stepping through records in a recordset and allows access to the individual key/value pairs (or fields of the recordset). What I want to do is very similar, except instead of passing a query/recordset and having it use the mysql_fetch functions to get an array to parse, I instead want to simply pass it an array and have it iterate through that in much the same way, exposing the key/value pairs (or again, fields) for me to access..

I hope this makes sense - please let me know if it doesn't and I'll try to explain further..

Thanks..

Comment #2

If you also want to see the keys you just need to change the example below to:.

21: foreach ( $a_row as $key=>$field ) {.

22: print "$key: $field<br>\n";.

23: }.

24: }.

But you already know the $key because that is the column name from the database....

So if I have this correct and you would use:.

While ( $a_row = mysql_fetch_row( $result ) ) {.

Print "Product id =". $a_row['products_id'] ." and Product name = "$a_row['products_name']. "<br>\n";.

}.

It should give you six lines with "Product id = etc.... ".

Or do I still don't understand what you want to do?..

Comment #3

Not exactly, but I really appreciate you staying with me and trying..

The sample you provided below still uses the mysql_fetch_row function, and passes it $result, which is what the query is set to. the $result variable is the recordset returned from the query. If I were trying to parse a recordset from the query, your samples would work, but I have actually already done that. See I'm running two queries. The first one uses a similar function, mysql_fetch_assoc on the result which returns an associative array. If there are fewer than six rows returned from the database, the I run the second query and get more values, also converting that result into an associative array using the mysql_fetch_assoc function.

It is this combined array that I now must parse with the loop. The example you provided expects a query result as the input and I need to provide an array as the input. I basically want exactly what you listed in your last post, but instead of parsing and iterating through a query result, I need to do it with an associative array...

Comment #4

Then that is the way it is done...IMHO.

Foreach ( $a_row as $key=>$field ) {.

Print "$key: $field<br>\n";.

}.

The key is associated with, well the key and $field is the value associated with it..

I see I made a big mistake: in the example I gave I shouldn't have used mysql_fetch_row but mysql_fetch_array to get an associative array.....

So:.

While ( $a_row = mysql_fetch_row( $result ) ).

Should have been.

While ($a_row = mysql_fetch_array( $result) )..

Comment #5

The above code just iterates through the fields and returns the values for each field, so your print statement would in my case print three lines (one for each field), listing the field name, then the value associated with it but just for the first record. I need to access the field values for all the records, so instead of the loop iterating through each field, it needs to iterate through each record, giving me access to each field's value for that record..

Mysql_fetch_array, mysql_fetch_assoc, mysql_fetch_row, mysql_fetch *anything will not work because all of these functions expect a recordset direct from a query. Remember, I'm trying to loop through an array, which for some reason is different than the recordset..

Thanks.

Scott..

Comment #6

Actually, as far as my knowledge goes, it is exactly the same thing. Could it be that keys and fields got reversed in the array_merge? Anyway, it would probably be insightfull to merge and then add to your code:.

<pre>.

Print_r($the_new_array_that_was_merged);.

</pre>.

That will give you a print of the array: fields and keys...

Comment #7

I ran into the same problem not to long ago and it was suggested that I use.

Instead of foreach. Works for me..

Jack..

Comment #8

Actually I did some testing just using the single unmerged array. So for that test I first set a variable equal to mysql_fetch_assoc($result) where $result = mysql_query(query), then I tried to do the foreach with that variable and as I explained before it returned only three values, one for each field. So the foreach was iterating through the fields instead of the rows..

Jack_mcs:.

Thanks for the contribution, however please see posts above - the suggestion you are making requires $result to be the direct result of a query. This is not what I want. Instead I need to iterate through an array - what is essentially a multidimensional associative array..

Anything that involves any of the fetch functions will not work..

Thanks..

Comment #9

Here's what you said above:.

Maybe I am misunderstanding the terminology, but the way I understand it is that each entry in a table is considered a record. Each record will have various fields, named, for example, products_id. By doing the followng, you can access each and every record from the specified table:.

$result_query = tep_db_query("select products_id from "TABLE_SOME_TABLE) or die("Query failed");;.

While ($result = tep_db_fetch_array($result_query)) {.

 echo 'Product ID = '.$result['products_id'].'<br>';.

}.

Is this not what you want to do?.

Jack..

Comment #10

No. the difference between what you have suggested and what I want to do has to do with the input to the loop. If you look at your code, you are using the tep_db_fetch_array function. If you look at that function in the database.php file under includes/functions you will see that it simply maps to the built in PHP function called mysql_fetch_array using the ASSOC switch. Using that switch is exactly the same as the function mysql_fetch_assoc, and both will accept a query recordset as input and return an associative array. So in your code you are setting the $result_query variable equal to tep_db_query(), meaning after that line executes, the $result_query variable holds the recordset returned from the array, and if you look at the docs, you will see mysql_query returns type "resource" (.

Http://us2.php.net/m...mysql-query.php.

).

Then in your code below, you set up a while loop and set $result equal to tep_db_fetch_array (which is the same as mysql_fetch_assoc), passing it the recordset created in the previous step. This has the result of setting the variable $result equal to an associative array, which is properly accessed within the loop and the while function somehow knows to iterate through the array of records so on each pass of the loop you see a different record, but have access to each field in the record the specifying the field name in brackets, like so: $result['product_id']..

Now, what I want to do is different because instead of passing a query's recordset into the tep_db_fetch_array, and in turn passing that result into the while loop, I need to pass it an array. Steps before I start the loop I have basically already done the part where I call the tep_db_fetch_array and create an array out of the recordset. It it this array that I have created steps before that I need to loop through. Unfortunately when I do that, instead of giving me a different record on each pass of the loop like I explained above, it gives me a different field of the first record only and then stops..

This really should be a simple php function - just can't figure out how to do it...

Comment #11

Bump...

Anyone?.

Is my explanation not clear? I would think this should be pretty simple....

Thanks..

Comment #12

Hi,.

I have searched all over the web for an answer and I must not be using the right search terms because I can't come up with anything. I am basically trying to replicate the automatic looping functionality you get with a statement like this:.

Where ($value = tep_db_fetch_array($arr)) {.

//code.

}.

OR.

Where ($value = mysql_fetch_assoc($arr)) {.

//code.

}.

I need to create that functionality using an array (and presumably foreach) instead of the mysql_fetch_assoc function. The reason for this is above this section I will be grabbing some values out of the database and turning that result into an array using the mysql_fetch_assoc function, but I will be getting two sets of values, creating two arrays, then combining them into one array to be processed by the loop. The most important feature I need is to be able to access the values by their keys, so for example using the above samples, in the code section I would need to make references to $value['products_id'] or $value['products_name']. Apparently the mysql_fetch_assoc function returns a special kind of array or something because the closest I have come is the following code:.

Foreach ($value as $k => $v) {.

Echo " TEST: $v";.

}.

The select statment that creates the array $value that I'm feeding into the foreach looks like this: "SELECT p.products_id, p.products_image, pd.products_name", and it returns six records. Instead of the foreach iterating through each of the six records, it iterates through each of the three fields so the echo statement above lists the values of those three fields for the first record..

How do I get it to loop through each of the records and let me access the fields by their key names? How do I get this to function just like the where ($array = mysql_fetch_assoc) statement?.

Thanks!.

This post has been edited by.

Luchysoft.

: 19 August 2004, 17:34..

Comment #13

I think you are looking for an example like this (this is from Matt Zandstra's Sams Teach Yourself PHP in 24 hours, recommended reading ;-)).

Http://www.samspublishing.com/articles/art...=29841&seqNum;=7.

1: <html>.

2: <head>.

3: <title>Listing 12.5 Listing all rows and fields in a table</title>.

4: </head>.

5: <body>.

6: <?php.

7: $user = "harry";.

8: $pass = "elbomonkey";.

9: $db = "sample";.

10: $link = mysql_connect( "localhost", $user, $pass );.

11: if ( ! $link ).

12:   die( "Couldn't connect to MySQL" );.

13: mysql_select_db( $db, $link ).

14:   or die ( "Couldn't open $db: ".mysql_error() );.

15: $result = mysql_query( "SELECT * FROM domains" );.

16: $num_rows = mysql_num_rows( $result );.

17: print "There are currently $num_rows rows in the table<P>";.

18: print "<table border=1>\n";.

19: while ( $a_row = mysql_fetch_row( $result ) ) {.

20:   print "<tr>\n";.

21:   foreach ( $a_row as $field ).

22:     print "\t<td>$field</td>\n";.

23:   print "</tr>\n";.

24: }.

25: print "</table>\n";.

26: mysql_close( $link );.

27: ?>.

28: </body>.

29: </html>.

Specific fields from $a_row can be accessed as: $a_row['products_id'] The foreach loop is only needed if you want to go through all the "fields"..

I hope this helps...

Comment #14


This question was taken from a support group/message board and re-posted here so others can learn from it.