[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

Post by paquin1 »

no problem, I'll be here, and thanks for your help :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

dethron wrote:thanx now, give some time :) just sit, and relax :)
You should encourage users to figure out the solution on their own.. not do it for them. :S... make it a better learning experience
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

by the way using int as a variable name, is not good :)
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Phenom, be sure that, i will do that :) just wait the next post ;)
i do not have a IIS with PHP support, i'm just cheking the $$, is OK for IIS?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

It's nice to see someone new helping out as much as you :)

keep up the good work..

back to the topic..
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

thnx, i love this forum :)
and i'm a very old member of it :)
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

I think you are right, and I don't expect other to do my work... yet I did what I need to get the job done, but it took lots of code. I'm hopping to learn how to code better by looking on how other do it.

on the IIS I don't think is working. because I still get errors..
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

im getting an
Notice: Undefined variable: Select one in c:\inetpub\wwwroot\test.php on line 43 --- the line for $$
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

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 :)

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";       
		}
	}
?>
Now you can use $jan, $feb, $mar as your variable.
Try it, if you get any error, then we can blame IIS with PHP :)
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Try the code above alone, it should be work.
$rows['news_id'] should be replaced by $arr[$i]->news_id
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

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?
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

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 ;)
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

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.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Your welcome, back again to forum soon :)

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

Post by paquin1 »

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.
:arrow: Creating new variables by adding an extra $ to the array or other variables... $$mes[$regs[2]]

:arrow: learn to understand the array more by displaying what mysql rows had and by printing what was inside of the arrays.

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
&lt;tr align="center"&gt;
      &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;
Post Reply