 |
|
|
02-16-2012, 04:43 PM
|
arrays and while loop
|
Posts: 112
Name: Joan
|
Hello,
I am using forge_fdf to output data from a DB into a pdf... it works for a single record, but right now i have a list of records max records will be 6. so what ia m trying to do is use a while loop, but the data is already in an array and im getting confused with this, if anyone knows how to solve this please help
here is what i have so far..
PHP Code:
$fi = mysql_query("SELECT * FROM `log_dressing` WHERE `viewing_date` = '".$vdate."'") or die(mysql_error());
while($fif = mysql_fetch_assoc($fi)){
$name = array("name1","name2","name3","name4","name5","name6");
$dname = $fif['deceased_name'];
$hora = $fif['viewing_time'];
$casket = $fif['casket'];
$ropa = $fif['confirm'];
echo $name . " => " . $dname . " ";//trying to see the output which is this
//Array => EVELYN PRISCILA Array => SALVADOR ROSALES
//why am i getting Array instead of name1 and name2?
//this is what i need to generate in order to output the data in the pdf form
$fdf_data_strings= array('name1' => $dname, 'name2' => $dname, 'name3' => $dname, 'name4' => $dname, 'name5' => $dname, 'name6' => $dname);
//i hope this makes sense thanks for your help
}
|
|
|
|
02-16-2012, 07:29 PM
|
Re: arrays and while loop
|
Posts: 1,091
Name: Paul W
|
while($fif = mysql_fetch_assoc($fi)){
$name-str = $name['name1'] . " " . $name['name2'] . " " . etc
$dname = $fif['deceased_name'];
.
.
.
}
with a bit of handling per value in the names in case they're NULLs.
|
|
|
|
02-17-2012, 12:50 PM
|
Re: arrays and while loop
|
Posts: 112
Name: Joan
|
that didnt work for me :/
i changed the code a bit but it is still not working..
PHP Code:
while($row = mysql_fetch_array($fi)){
$dname = $row['deceased_name']; $hora = $row['viewing_time']; $casket = $row['casket']; $ropa = $row['confirm'];
$keys = array('name1', 'name2'); $a = array_fill_keys($keys, $dname); print_r($a);
}
that outputs this:
Array ( [name1] => carlos [name2] => carlos ) Array ( [name1] => maria [name2] => maria )
but i need to output this:
Array ( [name1] => carlos [hora1] => 15:00:00 [casket1] => red [ropa1] => No [name2] => Maria [hora2] => 13:00:00 [casket2] => blue horizon [ropa2] => Yes )
this is because i know there are only 2 records but its not always 2 records it could be from 1 to 6, that another problem, if there are 5 recors it would have to go 'name3', 'hora3,'casket3','ropa3' and so on...
thanks for your help
thanks for your help
Last edited by stivens; 02-17-2012 at 12:53 PM..
|
|
|
|
02-17-2012, 01:28 PM
|
Re: arrays and while loop
|
Posts: 112
Name: Joan
|
hello, I have found a solution, but its a very long way and i think is just repetitive, i think there is a better way to do it, i'm just not sure how :/. if there is a better way to do it, any help would be really appreciate it thanks here is the code..
PHP Code:
$sql1 = "SELECT deceased_name,viewing_time,casket,confirm FROM `log_dressing` WHERE `viewing_date` = '".$vdate."' ORDER BY viewing_date DESC LIMIT 0,1"; $res1 = mysql_query($sql1) or die(mysql_error()); $row1 = mysql_fetch_array($res1);
$dname1 = $row1['deceased_name']; $hora1 = $row1['viewing_time']; $casket1 = $row1['casket']; $ropa1 = $row1['confirm'];
$sql2 = "SELECT deceased_name,viewing_time,casket,confirm FROM `log_dressing` WHERE `viewing_date` = '".$vdate."' ORDER BY viewing_date DESC LIMIT 1,2"; $res2 = mysql_query($sql2) or die(mysql_error()); $row2 = mysql_fetch_array($res2);
$dname2 = $row2['deceased_name']; $hora2 = $row2['viewing_time']; $casket2 = $row2['casket']; $ropa2 = $row2['confirm'];
$sql3 = "SELECT deceased_name,viewing_time,casket,confirm FROM `log_dressing` WHERE `viewing_date` = '".$vdate."' ORDER BY viewing_date DESC LIMIT 2,3"; $res3 = mysql_query($sql3) or die(mysql_error()); $row3 = mysql_fetch_array($res3);
$dname3 = $row3['deceased_name']; $hora3 = $row3['viewing_time']; $casket3 = $row3['casket']; $ropa3 = $row3['confirm'];
$fdf_data_strings = array('name1' => $dname1, 'hora1' => $hora1, 'casket1' => $casket1, 'ropa1' => $ropa1, 'name2' => $dname2, 'hora2' => $hora2, 'casket2' => $casket2, 'ropa2' => $ropa2, 'name3' => $dname3, 'hora3' => $hora3, 'casket3' => $casket3, 'ropa3' => $ropa3);
print_r($fdf_data_strings);
this is only for 3 i need to do three more because there might be 6 records
thanks..
Last edited by stivens; 02-17-2012 at 01:33 PM..
|
|
|
|
02-17-2012, 06:45 PM
|
Re: arrays and while loop
|
Posts: 845
Name: Mattias Nordahl
Location: Sweden
|
This should give you an idea of a solution:
PHP Code:
$data = array(); while($row = mysql_fetch_array($fi)){
$dname = $row['deceased_name']; $hora = $row['viewing_time']; $casket = $row['casket']; $ropa = $row['confirm'];
$data[] = array('name' => $dname, 'hora' => $hora, 'casket' => $casket, 'ropa' => $ropa);
}
print_r($data);
$data will now be an array containing arrays, one for each row from your database, in numeric order starting from zero. See the result from the print_r().
However, it could be done a bit easier. mysql_fetch_array() can fetch you either a numeric array, an associative array, or both. mysql_fetch_assoc() is just a shortened version that only fetches the associative array, which is what you want.
PHP Code:
$data = array(); while($row = mysql_fetch_assoc($fi)){ $data[] = $row; }
print_r($data);
With this approach, though, the keys in the arrays will be the same as those in the database (deceased_name instead of name, viewing_time instead of hora, etc.).
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
|
|
|
|
02-20-2012, 04:56 PM
|
Re: arrays and while loop
|
Posts: 112
Name: Joan
|
this is the output from the second code..
Array ( [0] => Array ( [deceased_name] => EVELYN [viewing_time] => [casket] => HORIZON [confirm] => Yes ) [1] => Array ( [deceased_name] => SALVADOR [viewing_time] => [casket] => [confirm] => ) )
if i cant change the keys this wouldnt work :/ there is no way to make the keys of the array and array?
thanks
|
|
|
|
02-20-2012, 05:28 PM
|
Re: arrays and while loop
|
Posts: 112
Name: Joan
|
I think I have found another solution i just have a little problem and i dont understand why...
PHP Code:
function array_combine_special($a, $b, $pad = TRUE) {
$acount = count($a);
$bcount = count($b);
// more elements in $a than $b but we don't want to pad either
if (!$pad) {
$size = ($acount > $bcount) ? $bcount : $acount;
$a = array_slice($a, 0, $size);
$b = array_slice($b, 0, $size);
} else {
// more headers than row fields
if ($acount > $bcount) {
$more = $acount - $bcount;
// how many fields are we missing at the end of the second array?
// Add empty strings to ensure arrays $a and $b have same number of elements
$more = $acount - $bcount;
for($i = 0; $i < $more; $i++) {
$b[] = "";
}
// more fields than headers
} else if ($acount < $bcount) {
$more = $bcount - $acount;
// fewer elements in the first array, add extra keys
for($i = 0; $i < $more; $i++) {
$key = 'extra_field_0' . $i;
$a[] = $key;
}
}
}
return array_combine($a, $b);
}
while($row = mysql_fetch_array($res1)){
$dname = array($row['deceased_name']);
$hora = $row['viewing_time'];
$casket = $row['casket'];
$ropa = $row['confirm'];
$names_arr = array('name1','name2');
$data = array_combine_special($names_arr, $dname);
//('name' => $dname, 'hora' => $hora, 'casket' => $casket, 'ropa' => $ropa);
}
print_r($data);
this is the output
Array ( [name1] => SALVADOR [name2] => )
Salvador should be the second record, why did it skip the first one? i don't get it :/
|
|
|
|
02-21-2012, 07:54 AM
|
Re: arrays and while loop
|
Posts: 845
Name: Mattias Nordahl
Location: Sweden
|
I don't see why my solution wouldn't work for you. If you want to keep the keys as "name", "hora" etc. rather than "deceased_name", "viewing_time" etc. just use the first code I posted instead. It will give you an array of arrays, where all the inner arrays are just as you said you wanted them.
You can then access them by index. So to access the first array (the first row from the database), use $data[0]. To access the second one use $data[1], and so on.
Each of these inner arrays will look like you first described them:
Code:
array(
'name' => 'Example name',
'hora' => '13:56:12',
'casket' => 'Example casket',
'ropa' => 'Yes'
);
And if you want to access a specific field from a certain row, specify indeces for both the outer and inner arrays, as in $data[0]['name'] or $data[3]['ropa'].
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
Last edited by lizciz; 02-21-2012 at 07:56 AM..
|
|
|
|
02-21-2012, 04:15 PM
|
Re: arrays and while loop
|
Posts: 112
Name: Joan
|
Yes you are right the first example you gave me would work for the first row of records, but what happens if there are more than one record?
PHP Code:
//this is one record
array(
'name' => 'Example name',
'hora' => '13:56:12',
'casket' => 'Example casket',
'ropa' => 'Yes'
);
//this is the output with two records
Array ( [name] => EVELYN [hora] => 10:00:00 [casket] => HORIZON [ropa] => Yes ) Array ( [name] => SALVADOR [hora] => example hour2 [casket] =>example casket2 [confirm] => No )
the output would be two arrays, I am using forge_dfd to generate a pdf with info from the database,
PHP Code:
//data that goes to the form
$fdf_data_strings = array('date' => format_date($vdate), 'name1' => $dname1, 'hora1' => $hora1, 'ataud1' => $ataud1, 'ropa1' => $ropa1, 'name2' => $dname2, 'hora2' => $hora2, 'ataud2' => $ataud2, 'ropa2' => $ropa2, 'name3' => $dname3, 'hora3' => $hora3, 'ataud3' => $ataud3, 'ropa3' => $ropa3);
|
|
|
|
02-22-2012, 11:26 AM
|
Re: arrays and while loop
|
Posts: 845
Name: Mattias Nordahl
Location: Sweden
|
Aha, now I see what you mean. I think this should work for you then.
PHP Code:
$data = array( 'dname' = array(), 'hora' = array(), 'casket' = array(), 'ropa' = array() ); while($row = mysql_fetch_array($fi)){ $data['dname'][] = $row['deceased_name']; $data['hora'][] = $row['viewing_time']; $data['casket'][] = $row['casket']; $data['ropa'][] = $row['confirm']; }
$correct = array(); foreach ($data as $key => $values) { $i = 1; foreach ($values as $val) { $correct[$key . $i++] = $val; } }
print_r($correct);
It can be done a bit shorter if the order doesn't matter, but I don't know wheater or not it does.
This should give you a single array that looks like:
Code:
Array(
'dname1' => "Example name 1",
'dname2' => "Example name 2",
'dname3' => "Example name 3",
...
'hora1' => "Example time 1",
'hora2' => "Example time 2",
'hora3' => "Example time 3",
...
and so on...
)
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
Last edited by lizciz; 02-22-2012 at 11:29 AM..
|
|
|
|
02-22-2012, 02:36 PM
|
Re: arrays and while loop
|
Posts: 112
Name: Joan
|
WOW!!!! your the best!!
that works perfect!!! the order does not matter. i would have never come up with that solution and that was exactly what i was looking for... i think i need to get a better understanding about arrays and foreach loops...
I was trying to understand how the code you wrote works, i think i get it until this point..
$data['dname'][] = $row['deceased_name'];
whats the purpose of having the second square braket blank?
thanks so much for your help!!
Quote:
Originally Posted by lizciz
Aha, now I see what you mean. I think this should work for you then.
PHP Code:
$data = array(
'dname' = array(),
'hora' = array(),
'casket' = array(),
'ropa' = array()
);
while($row = mysql_fetch_array($fi)){
$data['dname'][] = $row['deceased_name'];
$data['hora'][] = $row['viewing_time'];
$data['casket'][] = $row['casket'];
$data['ropa'][] = $row['confirm'];
}
$correct = array();
foreach ($data as $key => $values) {
$i = 1;
foreach ($values as $val) {
$correct[$key . $i++] = $val;
}
}
print_r($correct);
It can be done a bit shorter if the order doesn't matter, but I don't know wheater or not it does.
This should give you a single array that looks like:
Code:
Array(
'dname1' => "Example name 1",
'dname2' => "Example name 2",
'dname3' => "Example name 3",
...
'hora1' => "Example time 1",
'hora2' => "Example time 2",
'hora3' => "Example time 3",
...
and so on...
)
|
|
|
|
|
02-22-2012, 02:52 PM
|
Re: arrays and while loop
|
Posts: 845
Name: Mattias Nordahl
Location: Sweden
|
When dealing with arrays, you would normally give an index (or key) to specify a certain position in the array, as I'm sure you know. Such as
PHP Code:
$my_array[3] = 18;
to set a value in the fourth position of the array. If you do not give an index, however, just empty brackets, php will automatically use the next numerical index (starting at zero) in the array. For example, these two code blocks will yield the same result.
PHP Code:
$a = array(); $a[0] = "hey"; $a[1] = "there";
$b = array(); $b[] = "hey"; $b[] = "there";
It works the same way when using multi dimensional arrays, as in my code above, where there are 2 dimensions.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
|
|
|
|
02-22-2012, 02:56 PM
|
Re: arrays and while loop
|
Posts: 845
Name: Mattias Nordahl
Location: Sweden
|
By the way, if the order doesn't matter, you can reduce it to a single loop, like so:
PHP Code:
$data = array(); $i = 1; while($row = mysql_fetch_array($fi)){ $data['dname' . $i] = $row['deceased_name']; $data['hora' . $i] = $row['viewing_time']; $data['casket' . $i] = $row['casket']; $data['ropa' . $i] = $row['confirm']; $i++; }
print_r($data);
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
|
|
|
|
02-22-2012, 06:44 PM
|
Re: arrays and while loop
|
Posts: 112
Name: Joan
|
Awesome! thanks so much!
Quote:
Originally Posted by lizciz
By the way, if the order doesn't matter, you can reduce it to a single loop, like so:
PHP Code:
$data = array();
$i = 1;
while($row = mysql_fetch_array($fi)){
$data['dname' . $i] = $row['deceased_name'];
$data['hora' . $i] = $row['viewing_time'];
$data['casket' . $i] = $row['casket'];
$data['ropa' . $i] = $row['confirm'];
$i++;
}
print_r($data);
|
|
|
|
|
|
« Reply to arrays and while loop
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|