473,410 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

problem with "if-else" statement

[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
1 2004
bevort
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

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

Similar topics

4
by: lawrence | last post by:
Google can't find me a good example of how to use the "if exists" syntax in MySql. Is it right that to use it this way: INSERT INTO IF EXISTS tMyTable VALUES("xxlk", "lkjlkjlkjljk") I want...
10
by: george young | last post by:
I had developed the habit of using the neat python form: if someinstance: someinstance.memb() because it seems cleaner than "if someinstance is not None". {please no flames about "is not None"...
0
by: Emmett | last post by:
The transform below should create a dropdown where the entry for "SSS Building I" is selected. (Or am I missing something? ) This transform creates the dropdown but doesn't do the select. Can...
5
by: kmunderwood | last post by:
I am trying to combine "if match=" and "when test" I am a newbie, and have made both work separately, but I can not seem to combine them. This is my xml("index.xml")page(I can not change this,...
10
by: qazmlp | last post by:
There are some blocks of C/C++ code put under #if 0 #end if Is there anyway to make the code inside these blocks to get executed (may be by using some command line options)?
0
by: eyalhz | last post by:
I have this code at button click: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If ListBox1.SelectedIndex() Then Dim lines As...
6
by: vl106 | last post by:
A static code analysis tool gave me a warning on if (ptr && ptr->data) { ... } I assumed the tool doesn't get the "short circuit behaviour" in the if statement. But a collegue said it may...
1
by: MichaelZ | last post by:
After all is done, I want query(s) which will give me the ability to create a report which will allow me to compare 3 apts with the parameters equaling bedroom size, and rents which are equal or...
18
by: desktop | last post by:
I have 3 types of objects: bob1, bob2 and bob3. Each object is identified by a unique ID which gets returned by the function getId(). All bobs are descendants from class BaseBob which is an...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.