[solved]address 2 problems with syntax, full code provided

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

malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

fantastic, thats what i thought

you've both been a great help, Thank You
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You definitely need to look at the way you are looping - below is an example with lots of comments:

Code: Select all

<?php

if ((!$sortby) || (!$orderby) || (!$limitto))
{
	// state the fields you expect to retrieve from the database as it
	// makes it clearer when things go wrong or when someone else tries
	// to maintain your code
	$query = "SELECT username, email, register_date, access_level, DATE_FORMAT(last_login, '%H:%i - %d/%m/%y ') AS last_login FROM members ORDER BY access_level ASC";

	// use the or die() statement here - not after the SQL statement itself
	// as here it will do the job you need it to do:
	$result    = mysql_query($query) or die(mysql_error().'<p>'.$query.'</p>');
	$nomembers = mysql_num_rows($result);

	// if you want to show all the results you need to start looping here,
	// instead of trying to get results one at a time:
	while ($row = mysql_fetch_assoc($result)) {
		// read the second link in my sig and find out why $row1[last_login] is
		// bad code

		// look where the if statement is - each time it will be dealing with
		// the current records data and won't only be set once
		if ($row['last_login'] == Null) {
			$row['last_login'] = 'Never';
		}

		// personally I believe multiline double (or single) quoted strings
		// to be the work of the devil, for large blocks of HTML break out
		// of PHP into HTML or use heredoc, yes heredoc can take a little
		// while to get used to but using multiline quoted strings is
		// horrid (note how in heredoc you don't have to bother with 
		// escaping all those double quotes):
		echo <<<END

<!-- bad HTML, FORM tags cannot go between TR tags. -->
<form action="profile-viewer.php" method="post" name="{$row1['username']}_form" id="{$row1['username']}_id">
<tr>
	<td class="usernamerow">
		{$row1['username']}
		<input name="username2pass" type="hidden" id="username2pass" value="{$row1['username']}" />
	</td>
	<td class="emailrow">{$row1['email']}</td>
	<td class="registeredrow">{$row1['register_date']}</td>
	<td class="activityrow">{$row1['last_login']}</td>
	<td class="usergrouprow">{$row1['access_level']}</td>
	<td class="buttonrow">
		<input class="adminbutton" name="{$row1['username']}button" type="submit" id="{$row['username']}button" value="More..">
	</td>
</tr>
</form>
END;
	}

}

?>
Mac
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

hmmm your code is certainly somewhat different to mine

i will try and analyze my code and 'format' it better in future projects

however i still dont feel the need to break out of PHP to go into HTML, as long as your comfortable working with HTML
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

malcolmboston wrote:however i still dont feel the need to break out of PHP to go into HTML, as long as your comfortable working with HTML
Use heredoc, those double quoted multiline strings are going to make your life a misery in the long run.

I commented my code so you could see why the differences exist.

Mac
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

to be honest i think i have just got into the knack of coding in a certain way but im definitely gonna look into heredoc and try and incorporate that into my scripts

Thank you for pointing out my mistakes :lol:
Post Reply