By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,705 Members | 2,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,705 IT Pros & Developers. It's quick & easy.

problem with "if-else" statement

P: 9
[PHP]
<html>
<head>
<title>Search Questions</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php

$query = '';
$jenis = '';


$searchtype=$HTTP_POST_VARS['searchtype'];
$searchterm=$HTTP_POST_VARS['searchterm'];
$searchterm=trim($searchterm);

if (!$searchtype || !$searchterm)
{
echo 'You haven\'t entered search details. Please try again';
exit;
}

$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);

error_reporting(0);
@ $db = mysql_pconnect('localhost', 'root', '');
mysql_select_db('sistem bank soalan');
$jenis = $_GET["jenis"];

if (!$db)
{
echo 'Error: Cannot connect to database!! ;p';
exit;
}

$query = "SELECT * FROM soalan WHERE ".$searchtype." LIKE '%".$searchterm."%'" ;

$result = mysql_query($query);


$num_results = mysql_num_rows($result);

echo '<p>Number of question found: '.$num_results.'</p>';

for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo '<p><strong>'.($i+1).'. No Soalan: ';
echo htmlspecialchars(stripslashes($row['id']));
echo'</strong><br />Soalan: ';
echo stripslashes($row['soalan']);
echo '<br />Jenis: ';
echo stripslashes($row['jenis']);


if ($jenis == "Objektif") {
echo '<br />A: ';
echo stripslashes($row['jwpA']);
echo '<br />B: ';
echo stripslashes($row['jwpB']);
echo '<br />C: ';
echo stripslashes($row['jwpC']);
echo '<br />D: ';
echo stripslashes($row['jwpD']);
echo '<br />Jawapan: ';
echo stripslashes($row['jwpObj']);
}
else {
echo '<br />Jawapan: ';
echo stripslashes($row['jwpSubj']);
}
echo '<br />Aras Kesukaran: ';
echo stripslashes($row['araskesukaran']);
echo '<br />Sesi Peperiksaan: ';
echo stripslashes($row['sesipeperiksaan']);
echo '<br />Bahagian: ';
echo stripslashes($row['bahagian']);
echo '<br />Add User: ';
echo stripslashes($row['adduser']);
echo '</p>';
}
?>
<p align="center"><font face="BatangChe"><strong> </strong></font></p>
</body>
</html>
[/PHP]

NOTES: My coding cannot funtion well. it doesnt work..urmm.. okay, let me explain bout it first.. there's a table name SOALAN in my system that contain some field such as id, soalan, jwpA, jwpB, jwpC, jwpD, jwpObj, jwpSubj, jenis(which is type), and many more. I've two types(jenis) of questions(soalan) in my system; which is "subjektif" and "objektif".

For type "Objektif", only jwpA, jwpB, jwpC, jwpD and jwpObj that exist the content (its empty for field jwpSubj for this type), meanwhile for type "Subjektif", its only exist the content for jwpSubj and empty for the others field(i mean jwpA, jwpB, jwpC, jwpD, jwpObj).

So, when i want to call the question(soalan) field, i want to make a if-else statement to call the different types of question; which is like in the above coding.. hv i make it clear...? i just don't know why it can't funtion well.. i think its because of the system didn't recognize which type the question would be.. hurm.. anyone.. please help me with this.......
Sep 14 '06 #1
Share this Question
Share on Google+
1 Reply


bevort
P: 53
First of all, dit you test the SQL in a tool like PHPMyAdmin or, when working locale, a stand alone query tool?
Make sure that you get the results from the database as you want them.

Second, do not name a field in a table the same as the table itself, you will run in trouble soner or later.

When looking at the beginning of the code it seems you search for a type and a question together. they must be given both by the user

This means you can adjust your SQL to search for the type and the question

Inyour input form make sure you use a select statement (either radio or dropdown) to make sure you always get either a "objektif" or "subjektif"
Than you do not need a LIKE statement in your SQL, its rather time consuming

Before you submit the query to the database echo it to your screen to see if it is complete. Test this query also in the databse itself to see howmany results you can expect.

Next just print everything. If this is all OK than start working with the IF:s
Sep 14 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.