Jump to content

Archived

This topic is now archived and is closed to further replies.

ivalea

Using switch with an if....?

Recommended Posts

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

[code]$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++;
}[/code]

Then right after that I have this:
[code]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.";
}[/code]

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

Share this post


Link to post
Share on other sites
line[code]$email2=mysql_result(result,$i,"customers_email_address");[/code]must be[code]$email2=mysql_result($result,$i,"customers_email_address");[/code]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
This is how I would do it.  You need a condition to join the tables though.

[code]
<?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>';

?>
[/code]

Share this post


Link to post
Share on other sites
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());

Share this post


Link to post
Share on other sites
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:

[code]
$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;
}[/code]


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...

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.