[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

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

Post by dethron »

can you send the dump schema of the db?
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

hold on I need to format my date coming from my db, cause right now the months come out like 01, 02, 03....
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

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

Post by paquin1 »

Code: Select all

<?php
$query = "SELECT file_id, DATE_FORMAT(file_date, '%Y %c %e') AS fd 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['fd'];
		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";
		}
}
?>
I get no errors now but it jumps to the else Invalid date format: $date
Last edited by paquin1 on Thu Sep 16, 2004 9:46 am, edited 1 time in total.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

dont forget $$ on line 13
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

$$mes[$regs[2]]
here, assume $regs[2] given us as 10,
then,

$$mes[10] becomes $oct

then you expect

$oct is "<option selected>Select one</option>\n<option value = \"download_file.php?uid={$rows['news_id']}\">$regs[3]</option>\n";
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

ok I got it not to jump to the else statement, by adding hyphens to my mysql '%Y-%c-%e', but if I add the extra $ to line 13 y get this error now..
Notice: Undefined variable: Select one in c:\inetpub\wwwroot\test.php on line 43 line 43 being line 13 of last code
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

no extra $ gives me no erros but also no data
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

should I also change somethig in my tables with my pulldown menus?
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

paste here the db dump schema, and so i can finish it :)
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

how do I do that?
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Code: Select all

<?php
$resultQ = mysql_query($resultSQL);
	
while($row = mysql_fetch_object($resultQ))
	$list[count($list)] = $row;

print_r($list);
?>
replace line 4 with the code above, and tell me what you see on your screen?

by the way, are you using MSSQL and IIS for php?
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

change you code to

Code: Select all

<?php
$query = "SELECT file_id, DATE_FORMAT(file_date, '%Y-%c-%e') AS fd FROM uploads";
//$result = mysql_query($query);
$resultQ = mysql_query($query);    
while($row = mysql_fetch_object($resultQ))    
$list[count($list)] = $row;
print_r($list);

?>
because I was just getting errors... and commented all of my PHP

and this is what I get on the screen

Code: Select all

Notice: Undefined variable: list in c:\inetpub\wwwroot\test.php on line 34
Array ( &#1111;0] =&gt; stdClass Object ( &#1111;file_id] =&gt; 1 &#1111;fd] =&gt; 2004-1-20 ) &#1111;1] =&gt; stdClass Object ( &#1111;file_id] =&gt; 2 &#1111;fd] =&gt; 2004-5-10 ) &#1111;2] =&gt; stdClass Object ( &#1111;file_id] =&gt; 3 &#1111;fd] =&gt; 2004-1-30 ) &#1111;3] =&gt; stdClass Object ( &#1111;file_id] =&gt; 4 &#1111;fd] =&gt; 2004-10-13 ) )
and I'm using MySQL and IIS to test and than I uploaded to a MySQL and linux server.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

thanx now, give some time :) just sit, and relax :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

undefined variable means that youy havn't properly set the variable before you are trying to use it

good way to do it

Code: Select all

<?php

$int = 'blah';

if ($int == 5) 
echo '5';
else 
echo 'no var';


?>
bad way

Code: Select all

<?php

if ($int == 5) 
echo '5';
else 
echo 'no var';


?>
Post Reply