Forum Moderators: coopster

Message Too Old, No Replies

PHP array from array

Embedding looping statements...

         

kieftrav

8:23 pm on Sep 17, 2006 (gmt 0)

10+ Year Member



What I'm trying to accomplish is taking each value from an array, finding matching values in a database from column1 and then returning the values from column2 and placing them in a new array. The trick is that each value in the original array may be expressed multiple times in its respective column but the supplemental fields are all different (two foreign keys creating a primary key.) So, here's the code that I have that I'm trying to work with. Is there some taboo about putting multiple loops together? Also, if I'm guessing correctly, the results will be in a two-tiered array... Is there another piece of script that can help me break it from a two-tier to just a one-tier?

$data[] --Array with multiple contents
$sql_2 = "SELECT intsch_id FROM interest_scholarships_interest WHERE interests=$character";
$result_2 = mysql_query($sql_2);
$data_2 = array();

foreach ($data as $character)
{
while ($row_2 = mysql_fetch_array($result_2))
{
$data_2[] = $row_2;
}
}

Thanks,
Travis

ahmedtheking

9:19 pm on Sep 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This can't work, because you're executing the query before it's in the loop! It's better to

foreach ($blah as $blah2) {
$val1 = "#*$!";
$xx = mysql_query("lalala ".$val1);

while ($r = mysql_fetch_array($xx)) { ....

and so on!

kieftrav

9:33 pm on Sep 17, 2006 (gmt 0)

10+ Year Member



Yes, ahmedtheking, I tried that as well, and I still get the same results. Any other ideas?

ahmedtheking

10:00 pm on Sep 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah yes, let's reconstruct the loop:

$data = range(0,12); // creates a 0-12 array, otherwise $data should be an array!
$data2 = array();
$data_new = array();
$generic_query = "SELECT intsch_id FROM interest_scholarships_interest WHERE interests=";

foreach ($data as $k => $character)
{
$query = $generic_query."'".$character."'";
$i = 0;
while ($r = mysql_fetch_array($query))
{
$data2[$i++] = $r['intsch_id']; // or $r['0']
}

// now add the data2 array into the data array
$data_new[$k] = $data2;

// delete data2
unset($data2);
}

kieftrav

10:43 pm on Sep 17, 2006 (gmt 0)

10+ Year Member



Hey, ahmedtheking, it looks like we're making progress... I made a change to the script you gave me... There wasn't a mysql_query statement, so I added it. I have a print_r statement to view the contents of the array and these are the results...

Array ( [0] => Array ( ) [1] => [2] => [3] => [4] => [5] => )

It doesn't have the values that I was hoping it would...

Also here is the updated script segment:

$data // array that has values
$data_2 = array();
$data_new = array();
$generic_query = "SELECT intsch_id FROM interest_scholarships_interest WHERE interests=";

foreach ($data as $k => $character)
{
$query_3 = mysql_query($generic_query."'".$character."'") or die(mysql_error());
$i = 0;
while ($r = mysql_fetch_array($query_3))
{
$data_2[$i++] = $r['intsch_id'];
}
$data_new[$k] = $data_2;
unset($data_2);
}
print_r($data_new);

eelixduppy

11:29 pm on Sep 17, 2006 (gmt 0)



To make things a little simpler, try something like this, although it should have a similar effect:

$data; // array that has values
$data_new = array(array());
$generic_query = "SELECT intsch_id FROM interest_scholarships_interest WHERE interests=";

foreach ($data as $k => $character)
{
$query_3 = mysql_query($generic_query."'".$character."'") or die(mysql_error());
while($r = mysql_fetch_array($query_3)){
$data_new[$k][] = $r['intsch_id'];
}
}
echo '<pre>';
print_r($data_new);
echo '</pre>';

My suggestion to you is to make sure that you are actually receiving results from your queries.

Hope this helps. Good luck!

kieftrav

2:27 am on Sep 18, 2006 (gmt 0)

10+ Year Member



Thanks, eelixduppy, but I still have an empty array as a result. I know $data has an array full of values. Does it make a difference if some of the values in the $data array don't have matches/results from the new mysql_query? Hmmm...

kieftrav

4:49 am on Sep 18, 2006 (gmt 0)

10+ Year Member



Does anyone else have any more ideas?

siMKin

5:15 am on Sep 18, 2006 (gmt 0)

10+ Year Member



What eelixduppy said before: "make sure that you are actually receiving results from your queries."

anyways, why are u using multiple queries and not just 1?

SELECT intsch_id FROM interest_scholarships_interest WHERE interests IN ('bla', 'di', 'bla')

or

SELECT intsch_id FROM interest_scholarships_interest WHERE interests='bla' OR interests='di' OR interests='bla'

seems a whole lot easier to me

Psychopsia

5:27 am on Sep 18, 2006 (gmt 0)

10+ Year Member



Is this part of your problem on the other topic [webmasterworld.com], where the $data is an array containing the "SELECT interests ..." query?

If that's true, I can give you my coding based on it, therefore you can change it if needed:


$data2 = array();
foreach ($data as $k => $drow)
{
$sql = "SELECT intsch_id FROM interest_scholarships_interest WHERE interests='" . $drow['interests'] . "'";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result))
{
$data2[$k][] = $row;
}
}

ahmedtheking

7:16 am on Sep 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, let's just try and check out what you're trying to achieve, again:

In array terms, is it something like this:

Array (
something => array (0, 1, 3, 4, n)
something2 => array(n, n+1, n+n)
etc...
)

Yeah?

Now, tell us what the values of '$data' is. Are they keyed? What's the relation with the values in the DB?

kieftrav

3:32 pm on Sep 18, 2006 (gmt 0)

10+ Year Member



Psychopsia hit it right on the nose... I'm trying to create quite a large dynamic website... Here's what I'm doing in terms of relations

TABLE_A

COLUMN_A ¦ COLUMN_B

A ¦ 1
A ¦ 2
A ¦ 3
A ¦ 4
B ¦ 1
B ¦ 3
B ¦ 4
C ¦ 2
C ¦ 3
C ¦ 4

This is where my last post was concerned in terms of wanting to dynamically get an array of column_b's field values that corresponded with a specific A values... The A values were stored as SESSION variables too... So it is specific to each person that logs in. Then, here is the second part.

TABLE_B

COLUMN_A ¦ COLUMN_B
0001 ¦ 1
0001 ¦ 2
0001 ¦ 3
0002 ¦ 1
0002 ¦ 3
0003 ¦ 2
0004 ¦ 3
0004 ¦ 4

Column_b references the same data in Table A as in Table B. Column A references a different table. Now, from the fields I received from column_b in table_a, I want to now pull the values from column_a in table_b. Also, is there a problem if multiple values from the array we received from table_a reference the same field in column_a (example: 1 & 2 in column_b reference 0001)? All right, and that's not everything... I then want to display a table full of information that has a primary key of table_b column_a's data and those results in tabular format with a master_detail_page setup... So, yes, I have quite an undertaking on my behalf. I haven't been coding that long and all of the references I've used barely ever talk about dynamic data to the extent that I want to use it... Does anyone have any ideas on what I can do in terms of research? I hate to be bothersome and ask all of you to do the coding for me, and that's why I'm trying to figure out steps on my own, but when I try to figure it out on my own and something doesn't work like I thought it would, boy is it frustrating! So, that's where I am... Anyone have ideas?

Travis

P.S. Should I just get a professional programmer?

kieftrav

6:30 pm on Sep 18, 2006 (gmt 0)

10+ Year Member



Hey Psychopsia,

Your suggestion worked. Any help for the rest of my last post? Sorry to be such a pest!

Travis

eelixduppy

3:32 am on Sep 19, 2006 (gmt 0)



Does anyone have any ideas on what I can do in terms of research?

Read lots of books on php and mysql, use the internet constantly ( [php.net...] and [mysql.com...] ) and the best resource of all, webmasterworld! This community is loaded with information, you just have to find it.

I hate to be bothersome and ask all of you to do the coding for me.

We are here to help you with your issues. By asking us about your problems not only are you learning, but we are learning as well. It's mutual thing ;)


Should I just get a professional programmer?

This is entirely up to you. If you are not up to the challenge, then sure, why not. But if you want to expand your mind, and learn something useful in the process, then keep at it! :)

Anyway, if you have any more questions regarding how you should go about this then don't hesitate to reply to this post.

Good luck!

kieftrav

7:01 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



Hey eelixduppy, thanks for the help. I appreciate everyone who helps. The reason why I'm considering getting a professional programmer is because I'm an entrepreneur who is on a tight schedule. I'd love to learn everything about web development, but there is only so much a person can learn in a 24 hour day!

Thanks, and I'll be posting regularly.

Travis