[SOLVED] Code works, but can it be reduce? or improved?
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Now we are go 
I cut the db connection part, and assigned the values manually. So you should have do some good work to practice.
I did a mistake by adding $pulldown to every member of $mess array. I fixed it now
Now you can use $jan, $feb, $mar as your variable.
Try it, if you get any error, then we can blame IIS with PHP
I cut the db connection part, and assigned the values manually. So you should have do some good work to practice.
I did a mistake by adding $pulldown to every member of $mess array. I fixed it now
Code: Select all
<?php
$arr[0]->file_id = 1; $arr[0]->fd = "2004-1-20";
$arr[1]->file_id = 2; $arr[1]->fd = "2004-5-10";
$arr[2]->file_id = 3; $arr[2]->fd = "2004-1-30";
$arr[3]->file_id = 4; $arr[3]->fd = "2004-10-13";
$pulldown = "<option selected>Select one</option>\n";
$mes = array(1=>'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec');
for($i=0; $i<count($arr); $i++){
$date = $arr[$i]->fd;
if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
$$mes[$regs[2]] = $pulldown.$$mes[$regs[2]]."<option value = "download_file.php?uid={$rows['news_id']}">$regs[3]</option>\n";
} else {
echo "<br>Invalid date format: $date";
}
}
?>Try it, if you get any error, then we can blame IIS with PHP
Ok it works, but with errors
Notice: Undefined variable: jan in c:\inetpub\wwwroot\test.php on line 43
Notice: Undefined variable: may in c:\inetpub\wwwroot\test.php on line 43
Notice: Undefined variable: oct in c:\inetpub\wwwroot\test.php on line 43
line 43 being the line 11 of last code...
and some how I don't know why but in my January pulldown menu I get double "Select one" and in my attempt with long code I did not get the first row from MySQL so it might be ISS that has some problems with this double "Select one" and missing the first row with my old long code.
can you tell me what the $$ means.. is this like saying grab $mes[$regs[2]] and make it all one new variable?
Notice: Undefined variable: jan in c:\inetpub\wwwroot\test.php on line 43
Notice: Undefined variable: may in c:\inetpub\wwwroot\test.php on line 43
Notice: Undefined variable: oct in c:\inetpub\wwwroot\test.php on line 43
line 43 being the line 11 of last code...
and some how I don't know why but in my January pulldown menu I get double "Select one" and in my attempt with long code I did not get the first row from MySQL so it might be ISS that has some problems with this double "Select one" and missing the first row with my old long code.
can you tell me what the $$ means.. is this like saying grab $mes[$regs[2]] and make it all one new variable?
i think IIS(PHP) has some problems to use $$.
why don't you install phptriad buddle, it works great on Windows.
Let me explain $$:
I do not know the grab, but YES, it makse a new variable, and also power to reach variables without knowing its name
$one = "apple";
$apple = "cat";
echo $$one; // displays cat
why don't you install phptriad buddle, it works great on Windows.
Let me explain $$:
I do not know the grab, but YES, it makse a new variable, and also power to reach variables without knowing its name
$one = "apple";
$apple = "cat";
echo $$one; // displays cat
well i try $$one and it works fine... but thanks a lot for your help, I will fix the code you posted to work with MySQL and play arround to see if I can eliminate all this errors. Some how I had an idea of using the for loop, but was not sure how to do it. I'll try some more and post anything I come up with.
Thanks again for your help.
Thanks again for your help.
Ok, I finally got it to work. I try so many things but this is what worked for me. I took off an extra $$mes[$regs[2]] and change the for loop for a do..while loop because some how with any other loop I will not get the first row from MySQL.
New things I learned.
Creating new variables by adding an extra $ to the array or other variables... $$mes[$regs[2]]
learn to understand the array more by displaying what mysql rows had and by printing what was inside of the arrays.
New things I learned.
Code: Select all
<?php
$query = "SELECT file_id, DATE_FORMAT(file_date, '%Y-%c-%e') AS fd FROM files";
$result = mysql_query($query);
$rows = mysql_fetch_array($result);
$pulldown = "<option selected>Select one</option>\n";
$mes = array(1=>'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec');
do {
$date = $rows['fd'];
ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs);
$$mes[$regs[2]] = $pulldown."<option value = "download_file.php?uid={$rows['file_id']}">$regs[3]</option>\n";
}while ($rows = mysql_fetch_array($result))
?>Code: Select all
// this is part of the code for the table
<tr align="center">
<td>January<br>
<select name="select" onChange="MM_jumpMenu('parent',this,0)"><?php echo $jan ?></select></td>
<td>February<br>
<select name="select" onChange="MM_jumpMenu('parent',this,0)"><?php echo $feb ?></select></td>
<td>March<br>
<select name="select" onChange="MM_jumpMenu('parent',this,0)"><?php echo $mar ?> </select></td>
</tr>