Jump to content


Photo

Using switch with an if....?


  • Please log in to reply
6 replies to this topic

#1 ivalea

ivalea
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 03 October 2006 - 06:03 PM

Hello -
I'm trying to display a menu based upon information in two tables.  I've inserted a query that looks like this:

$query = "select * from members, customers";
$result = @mysql_query ($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num){
$model=mysql_result($result,$i,"members_pmodel");
$first=mysql_result($result,$i,"members_fname");
$last=mysql_result($result,$i,"members_lname");
$company=mysql_result($result,$i,"members_company");
$address=mysql_result($result,$i,"members_address");
$city=mysql_result($result,$i,"members_city");
$state=mysql_result($result,$i,"members_state");
$zip=mysql_result($result,$i,"members_zip");
$email=mysql_result($result,$i,"members_email");
$phone=mysql_result($result,$i,"members_phone");
$email2=mysql_result(result,$i,"customers_email_address");

echo "";

$i++;
}

Then right after that I have this:
if ($email == $email2){
switch($model){
	case 'c1':
	echo tep_image(DIR_WS_IMAGES . 'arrow_green.gif') . ' <a href="' . tep_href_link(FILENAME_C1, '', 'SSL') . '">' . MY_COURSES_C1 . '</a>'; 
	break;
             case 'c2':
             echo tep_image(DIR_WS_IMAGES .'arrow_green.gif') . ' <a href="' . tep_href_link
(FILENAME_C2, '', 'SSL') . '">' . MY_COURSES_C2 . '</a>';	}
	}else{
	echo "You are not enrolled in any courses at this time.";
	}

What I am trying to do is to display a different menu based upon the model number in the members table - but first I need to check if the email address in the customers table exists in the members table.

I keep getting this error though:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/www/os.xephor.net/catalog/account.php on line 152

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/www/os.xephor.net/catalog/account.php on line 152

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/www/os.xephor.net/catalog/account.php on line 152

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/www/os.xephor.net/catalog/account.php on line 152

Any thoughts on how to fix this?  Perhaps my code is wrong?  Thanks! :)

#2 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 03 October 2006 - 06:12 PM

line
$email2=mysql_result(result,$i,"customers_email_address");
must be
$email2=mysql_result($result,$i,"customers_email_address");


#3 JayBachatero

JayBachatero
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationQueens NY

Posted 03 October 2006 - 06:16 PM

Also it's mysql_num_rows.
$num=mysql_numrows($result); -> $num=mysql_num_rows($result);
JayBachatero
SMF Developer && Converter Specialist

#4 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 03 October 2006 - 06:16 PM

You really shouldn't use mysql_result() in a loop, better to just use mysql_fetch_...()! Also, why do you assign a already filled array to another set of variables. That's all your doing is wasting resources! Last thing, $num=mysql_numrows($result);, should be $num=mysql_num_rows($result);! Please don't thing I'm being rude, I am only trying to help you do things in much better way. The PHP Manual tells you this, it also listed on the MySQL site.

me!

#5 JayBachatero

JayBachatero
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationQueens NY

Posted 03 October 2006 - 07:17 PM

This is how I would do it.  You need a condition to join the tables though.

<?php

$request = mysql_query("
	SELECT m.*, c.*
	FROM members AS m, customers AS c
	WHERE <<CONDITION TO JOIN TABLE ON>>");

$numRows = mysql_num_rows($request);

// Load the results to an array
$members = array();
while ($row = mysql_fetch_assoc($request))
{
	$members[] = array(
		'model' => $row['members_pmodel'],
		'first' => $row['members_fname'],
		'last' => $row['members_lname'],
		'company' => $row['members_company'],
		'address' => $row['members_address'],
		'city' => $row['members_city'],
		'state' => $row['members_state'],
		'zip' => $row['members_zip'],
		'email' => $row['members_email'],
		'phone' => $row['members_phone'],
		'email2' => $row['customers_email_address'],
	);
}
mysql_free_result($request);

// Setup the opening table and row.
echo '
	<table>
		<tr>
			<td>First Name</td>
			<td>Last Name</td>
			<td>Model</td>
			<td>Company</td>
			<td>Address</td>
			<td>City</td>
			<td>State</td>
			<td>ZIP</td>
			<td>Email</td>
			<td>Phone</td>
			<td>Email 2</td>
		</tr>';

if ($numRows > 0)
{
	// Output the results
	foreach ($members as $member)
		echo '
			<tr>
				<td>', $member['first'], '</td>
				<td>', $member['last'], '</td>
				<td>', $member['model'], '</td>
				<td>', $member['company'], '</td>
				<td>', $member['address'], '</td>
				<td>', $member['city'], '</td>
				<td>', $member['state'], '</td>
				<td>', $member['zip'], '</td>
				<td>', $member['email'], '</td>
				<td>', $member['phone'], '</td>
				<td>', $member['email2'], '</td>
			</tr>';
}
else
	echo '
		<tr>
			<td colspan="11">No records found!</td>
		</tr>';

// Close the table
echo '
	</table>';

?>

JayBachatero
SMF Developer && Converter Specialist

#6 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 03 October 2006 - 07:58 PM

Whenever you get an error likie this
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/www/os.xephor.net/catalog/account.php on line 152

it means you have an error in you Query...
so go check the syntax of your SQL query

best way is to get rid of the @ symbol in front of your mysql_query function
and put this at the end of that line....before the semicolon

or die(mysql_error());

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#7 ivalea

ivalea
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 03 October 2006 - 10:14 PM

Thanks everyone for the help - I do have it working now.  Thanks printf - after reading your post I realized that all i really needed was the model #.  Now the only problem I have is that if there is more than one row with an email address matching the email address on file how can I get it to show both c1 and c2?  Here's what I have now - works perfect but only returns one row:

$query = "select members.members_pmodel from members, customers where members.members_email = customers.customers_email_address";
$result = @mysql_query ($query);
$num=mysql_num_rows($result);
$i=0;
while ($i < $num){
$model=mysql_result($result,$i,"members_pmodel");

echo "";

$i++;
}
switch($model){
	case 'c1':
	echo tep_image(DIR_WS_IMAGES . 'arrow_green.gif') . ' <a href="' . tep_href_link(FILENAME_C1, '', 'SSL') . '">' . MY_COURSES_C1 . '</a>';
	break;
	case 'c2':
	echo tep_image(DIR_WS_IMAGES . 'arrow_green.gif') . ' <a href="' . tep_href_link(FILENAME_C2, '', 'SSL') . '">' . MY_COURSES_C2 . '</a>';
	break;
	}


I was getting errors trying to use mysql_fetch...() - this way it will at least return one row.  But if there are two or more rows containing the same email address I need it to return both rows.  Not sure what to change here...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users