[SOLVED] Code works, but can it be reduce? or improved?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

[SOLVED] Code works, but can it be reduce? or improved?

Post by paquin1 »

After researching and figuring things out, I came up with the solution on how to do what I wanted. Yet, some how I think the code can be change to be simpler or more efficient, maybe a function or a better selection of code.

So if anybody can help me reduce this code to something simpler I will appreciate it. One thing that I my post somewhere else is that some how my first row from my mysql_array is not displaying, so my file_id=1 never shows.. Don't know why but it just doesn't.


ok here is my code

Code: Select all

<?php
require_once ('mysql_connect.php');
$query = "SELECT file_id, file_date FROM uploads";
$result = mysql_query($query);
$rows = mysql_fetch_array($result);

$pulldown = "<option selected>Select one</option>\n";
$jan = $pulldown;
$feb = $pulldown;
$mar = $pulldown;
$apr = $pulldown;
$may = $pulldown;
$jun = $pulldown;
$jul = $pulldown;
$aug = $pulldown;
$sep = $pulldown;
$oct = $pulldown;
$nov = $pulldown;
$dic = $pulldown;

while ($rows = mysql_fetch_array($result)) {
$date = $rows['file_date'];
		if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
   			if ($regs[2] == 1) {
			$jan .="<option value = "download_file.php?uid={$rows['file_id']}">$regs[3]</option>\n";
			}
			if ($regs[2] == 2) {
			$feb .="<option value = "download_file.php?uid={$rows['file_id']}">$regs[3]</option>\n";
			}
			if ($regs[2] == 3) {
			$mar .="<option value = "download_file.php?uid={$rows['file_id']}">$regs[3]</option>\n";
			}
			if ($regs[2] == 4) {
			$apr .="<option value = "download_file.php?uid={$rows['file_id']}">$regs[3]</option>\n";
			}
			if ($regs[2] == 5) {
			$may .="<option value = "download_file.php?uid={$rows['file_id']}">$regs[3]</option>\n";
			}
			if ($regs[2] == 6) {
			$jun .="<option value = "download_file.php?uid={$rows['file_id']}">$regs[3]</option>\n";
			}
			if ($regs[2] == 7) {
			$jul .="<option value = "download_file.php?uid={$rows['file_id']}">$regs[3]</option>\n";
			}
			if ($regs[2] ==  {
			$aug .="<option value = "download_file.php?uid={$rows['file_id']}">$regs[3]</option>\n";
			}
			if ($regs[2] == 9) {
			$sep .="<option value = "download_file.php?uid={$rows['file_id']}">$regs[3]</option>\n";
			}
			if ($regs[2] == 10) {
			$oct .="<option value = "download_file.php?uid={$rows['file_id']}">$regs[3]</option>\n";
			}
			if ($regs[2] == 11) {
			$nov .="<option value = "download_file.php?uid={$rows['file_id']}">$regs[3]</option>\n";
			}
			if ($regs[2] == 12) {
			$dec .="<option value = "download_file.php?uid={$rows['file_id']}">$regs[3]</option>\n";
			}
			
		} else {
   		 echo "Invalid date format: $date";
		}
}
?>
and here is some of the code of my HTML form in tables

Code: Select all

&lt;td&gt;January&lt;br&gt;
	  	&lt;select name="select" onChange="MM_jumpMenu('parent',this,0)"&gt;&lt;?php echo $jan ?&gt;&lt;/select&gt;&lt;/td&gt;
      &lt;td&gt;February&lt;br&gt;
        &lt;select name="select" onChange="MM_jumpMenu('parent',this,0)"&gt;&lt;?php echo $feb ?&gt;&lt;/select&gt;&lt;/td&gt;
      &lt;td&gt;March&lt;br&gt;
        &lt;select name="select" onChange="MM_jumpMenu('parent',this,0)"&gt;&lt;?php echo $mar ?&gt; &lt;/select&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr align="center"&gt;
      &lt;td&gt;April&lt;br&gt;
        &lt;select name="select" onChange="MM_jumpMenu('parent',this,0)"&gt;&lt;?php echo $apr ?&gt;&lt;/select&gt;&lt;/td&gt;
      &lt;td&gt;May&lt;br&gt;
        &lt;select name="select" onChange="MM_jumpMenu('parent',this,0)"&gt;&lt;?php echo $may ?&gt;&lt;/select&gt;&lt;/td&gt;
      &lt;td&gt;June&lt;br&gt;
        &lt;select name="select" onChange="MM_jumpMenu('parent',this,0)"&gt;&lt;?php echo $jun ?&gt;&lt;/select&gt;&lt;/td&gt;
Last edited by paquin1 on Fri Sep 17, 2004 10:21 am, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use arrays more...

make an array of the months, use the matched numbers to index into the array and use loops to print it all out.
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

ok I have added and array

Code: Select all

<?php
$mes = array(1=>'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec');
?>


but the rest is where I'm having a problem... is there a command or code to match numbers? I found preg_match_all is this what I use? or is it something like if ($regs[3] ==$mes[]) than do something else? [/php_man]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

reading how arrays work would help: http://www.php.net/manual/en/language.types.array.php
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Hi;
i haven't check your code completely, i just add a for loop, and i think it become much easier to do some changes :)

There may be some problems, then let me know, and i'll give more attention to issue :)

Hope following code help,

Code: Select all

<?php
	require_once ('mysql_connect.php');
	$query = "SELECT file_id, file_date FROM uploads";
	$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');
	
	for($i=1; $i<count($mes)+1; $i++)
		$mes[$i] = $pulldown;
	
	
	while ($rows = mysql_fetch_array($result)) {
		$date = $rows['file_date'];
	        if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {               
				$$mes[$regs[2]] .="<option value = "download_file.php?uid={$rows['file_id']}">$regs[3]</option>\n";
        	} else {           
				echo "Invalid date format: $date";
		}
	}
?>
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Oh, I almost forget to tell about the line 16 (in my post). I think it is what you need :) Use $$
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that doesn't work if the value is outside the array's range of elements though..
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Hi Feyd;
But as i understand from the code at the first post, i shouldn't do anything if value comes from outside of the array, right?

By the way, how can you copy-paste-test others code? When i tried i just lost \n's. And the code seem to me on 2 or 3 lines :(
When i was not here, code-parts in the post is upgraded (i think)
Anyway, it is good to be here again.
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

Ok I try what dethron say and yes it doesn't work I get
Notice: Undefined index: 05 in c:\inetpub\wwwroot\test.php on line 43

Notice: Undefined index: 01 in c:\inetpub\wwwroot\test.php on line 43

I did read on how arrays work, and the only thing i could find, that I'm about to try is 'foreach', I don't know if that is what you were thinking when you send me to the arrays but I will try this and see what happens.

:)
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

where is "on line 43"? in my post, there are just 21-lines.
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

is line 16 on your post
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

As feyd noticed the value of $regs[2] may come out of array, you should check this, try to echo it and tell us the value of it.
Should this value be considered to?
If so lets improve the code, if not then put a if-else check ;)
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Remember, regs[2] must be bigger than 0 and smaller than count($mes)+1 :)
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

ok I get a 5 and a 1 and looking on my db I have only 3 rows with this numbers two for January and one for may, which I think this is where these numbers come out.

so yes, I'm trying to match the month so if a row in mysql is for January than it should send that information to my pull down menu for january and not for any other month.
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

just to clarify I only have 3 rows total in my db for testing. so i'm sure I'll get Undefined index: XX for each month
Post Reply