PDA

View Full Version : Looping & Arrays


lux
12-15-2006, 01:51 PM
I have two arrays:

$one = array(a,b,c,d);
$two = array(1,2,3,4);

how can i loop through theses to achieve this result:

a: 1234
b: 1234
c:1234
d:1234

thanks

davidj
12-15-2006, 02:15 PM
you need a multi dimentional array

$one = array(a=>"1234", b=>"1234", c=>"1234", d=>"1234");

lux
12-15-2006, 02:33 PM
i've got this which works:

$one = array(a,b,c,d);
$two = array(1,2,3,4);

foreach($one as $first)
{
echo $first."-";
foreach($two as $second)
{
echo $second.",";
}
echo "<br>";
}

the real goal of what i am trying to do is create two arrays from two seperate sql statments and return the results in the format in my first post.

Recordset One:
$printer_query = "select * from tbl_printer";
$printer_result = mysql_query($printer_query);
$printer_rows = mysql_fetch_array($printer_result);
$printer_num_rows = mysql_num_rows($printer_result);

(list of printers)

Recordset Two:

$list_query = "select * from tbl_deptprinter where deptRef=". $_GET['deptId'];
$list_result = mysql_query($list_query);
$list_rows = mysql_fetch_array($list_result);
$list_num_rows = mysql_num_rows($list_result);

(list of reference ids from a compisite table where deptRef is equal to the variable passed in the URL)

I want to display the results in this format:

Printer 1
1
2
3
4

Printer 2
1
2
3
4

Printer 3
1
2
3
4

Any ideas?

davidj
12-15-2006, 02:35 PM
LUX

your banned

i have to actually think about your f*%&kn questions

lux
12-15-2006, 02:38 PM
i cold sweat came over me when i read YOUR BANNED, hehe,

i guess im only tiring to keep the forum on their toes! haha

davidj
12-15-2006, 02:41 PM
i guess im only tiring to keep the forum on their toes!


im like a ballerina!!!!

lux
12-15-2006, 02:45 PM
hahahahaha!

davidj
12-15-2006, 03:46 PM
I know what your trying to do !!!!!

you are looking for a method where you have this as a recordset


record1
sub_record1
sub_record2
sub_record3

record2
sub_record1
sub_record2
sub_record3

record3
sub_record1
sub_record2
sub_record3

record4
sub_record1
sub_record2
sub_record3


am i right?

this may have to wait til tomorrow!

lux
12-15-2006, 05:48 PM
i'm actually trying to attack the old problem of updating printers that belong to a department in a many to many scenario.

I have three tables which relate to one another like this:

tbl_dept -< tbl_deptprinter >- tbl_printer

(-> = one to many relation)

The user clicks on a link for a department from a list of departments on the first form. The dept id is passed in the URL.

The second page has a form showing all the details for that department.

I want to return a list of printers in the form, from tbl_printers, each of which has a check box next to them.

If a record in tbl_deptprinter matches both the printer id and dept id (passed in the URL) then it should show with a tick in it (CHECKED/SELECTED).

To do this i need to combine the recordset that returns the list of printers and one that lists all records in the compisite table(deptprinter) where deptRef=deptId passed in the URL.

So my logic would say:

-loop through the printer table and return each row
-display printer name
- loop through the deptprinter table where foreign key(deptRef)=primary Key(deptId)
- if foreign key(printerRef) = primaryKey(printerId) then checkbox selected
-else checkbox not selected

i just cant work my arrays in a way that produces the correct result.

lux
12-16-2006, 02:39 PM
different example but i have done it with one array from a db recordset and a static array, works well! hehe



/////////////////////////////////////////////////////
$pitch_query = "select * from pitch";
$pitch_result = mysql_query($pitch_query);
//$pitch_row = mysql_fetch_array($pitch_result);
$pitch_num_rows = mysql_num_rows($pitch_result);
/////////////////////////////////////////////////////
$time = array('10:30','11:30','12:30');

echo "No of pitches: " .$pitch_num_rows. "<br /><br />";

echo "<table border=\"1\">";
while ($pitch_row = mysql_fetch_array($pitch_result)){
echo "<tr>";
echo "<td>" .$pitch_row['pitch_name']. "</td>";
foreach($time as $t){
echo "<td>". $t. "</td><td><input name=\"radiobutton\" type=\"radio\" value=" .$t.$pitch_row['pitch_name']. " /></td>";
} echo "</tr>";
} echo "</table>";


(i going to do this even if it kills me!) hehe