473,468 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

DysFunctional

Somehow hours of typing have blinded me. Basically I'm trying to come
up with an "If not this then this" type scenario but (as you can
probably see) I am not getting what I want. All variables are good
(queries, etc.)

I want either "CANT GO HERE" or "GO HERE" but instead I end up with

"CAN'T GO HERE"
"GO HERE"
"GO HERE"
"GO HERE"

Of course I know the while loop is giving me the reiterations but I'm
not sure why I'm getting both sets back instead of one. Thoughts
[other than giving up PHP] ??

***************************
// This Function is KILLING ME
function didi_answer($qid,$userid)
{
include "db.inc.php";
$qstatus = mysql_query("SELECT status FROM `questions` WHERE `qid` =
'$qid'", $db);
$query1 = mysql_fetch_array($qstatus);
// Query 2 and 1
$result = "SELECT `a_uid` FROM `answers` WHERE `qid` = '$qid'";
$query = mysql_query($result, $db);
while ($item = mysql_fetch_array($query)) {
if (in_array($userid,$item)) {
echo "<br><br>CANT GO HERE";
}
if ( $query1['status'] = '1'){
echo "<br><br><a href='apage.php?qid=$qid'>GO HERE</a>";
}
}

************************

May 19 '07 #1
2 1085
At Sat, 19 May 2007 07:02:05 -0700, Akhenaten let his monkeys type:
Somehow hours of typing have blinded me. Basically I'm trying to come
up with an "If not this then this" type scenario but (as you can
probably see) I am not getting what I want. All variables are good
(queries, etc.)

I want either "CANT GO HERE" or "GO HERE" but instead I end up with

"CAN'T GO HERE"
"GO HERE"
"GO HERE"
"GO HERE"

Of course I know the while loop is giving me the reiterations but I'm
not sure why I'm getting both sets back instead of one. Thoughts
[other than giving up PHP] ??

***************************
// This Function is KILLING ME
function didi_answer($qid,$userid)
{
include "db.inc.php";
$qstatus = mysql_query("SELECT status FROM `questions` WHERE `qid` =
'$qid'", $db);
$query1 = mysql_fetch_array($qstatus);
// Query 2 and 1
$result = "SELECT `a_uid` FROM `answers` WHERE `qid` = '$qid'";
$query = mysql_query($result, $db);
while ($item = mysql_fetch_array($query)) {
if (in_array($userid,$item)) {
echo "<br><br>CANT GO HERE";
}
if ( $query1['status'] = '1'){
echo "<br><br><a href='apage.php?qid=$qid'>GO HERE</a>";
}
}

************************

It's not even fully clear to me what you want exactly.
I assume the following is what you logically mean:

If ($qid is in the db) //question ID exists
{
If ($userid is in a database row with the right question ID)
// User has already had this question
{
echo "Cannot go here";
}
else
{
echo <link to question>
}
}

If that's the case (see why choosing meaningful varnames and DOCUMENTING
are so important?) your solution could look like this:

function didi_anser($user_id, $qid)
{
include 'db.inc.php'; // opens a connection

$query = "SELECT status, a_uid FROM answers WHERE qid = '$qid'";
$result = mysql_query($query,$db);

if (mysql_num_rows($result)===0) {
// no rows present, question does not exist in db
some_error_handling("Wrong $qid message");
exit;
}

// question $qid exists in db, 1 or more rows to process

while ($row = mysql_fetch_assoc($result)) {
// I have my doubts about this, could there ever be more
// than 1 matching row for a single $qid ????
if ($row['status' === 1) { // Question is valid/active?
if ($user_id == $row['a_uid']) { // Already answered?
echo "You cannot go here";
} else {
echo "<a href='apage.php?qid=$qid'>GO HERE</a>";
}
} else {
// Status != 1
some_error_handling("Status related message");
}
}
}

I may be completely off the mark here, but I can't make anything else from
your code that actually makes a lot of sense.

BTW, use == when comparing variables' contents, === to assert both type
and contents are equivalent. = is always an assignment, so
if ($var = 1) returns a boolean TRUE (the assignment was succesful) and
leaves $var with value 1.

Again, please document your code, properly indent, use a lot of whitespace,
choose meaningful variable names, and try not to get sloppy:

if (in_array ($user, $item)) may return the correct value here, but it
isn't clear from seeing it what you are after.

if ($user_id == $row ['userid']) makes much more sense and is logically
the correct comparison.

HTH

Sh.
May 19 '07 #2
Akhenaten wrote:
Somehow hours of typing have blinded me. Basically I'm trying to come
up with an "If not this then this" type scenario but (as you can
probably see) I am not getting what I want. All variables are good
(queries, etc.)

I want either "CANT GO HERE" or "GO HERE" but instead I end up with

"CAN'T GO HERE"
"GO HERE"
"GO HERE"
"GO HERE"

Of course I know the while loop is giving me the reiterations but I'm
not sure why I'm getting both sets back instead of one. Thoughts
[other than giving up PHP] ??

***************************
// This Function is KILLING ME
function didi_answer($qid,$userid)
{
include "db.inc.php";
$qstatus = mysql_query("SELECT status FROM `questions` WHERE `qid` =
'$qid'", $db);
$query1 = mysql_fetch_array($qstatus);
// Query 2 and 1
$result = "SELECT `a_uid` FROM `answers` WHERE `qid` = '$qid'";
$query = mysql_query($result, $db);
while ($item = mysql_fetch_array($query)) {
if (in_array($userid,$item)) {
echo "<br><br>CANT GO HERE";
}
if ( $query1['status'] = '1'){
echo "<br><br><a href='apage.php?qid=$qid'>GO HERE</a>";
}
}

************************
You are getting two sets of data returned for each query.

mysql_fetch_array(...) returns an array consisting both ASSOCIATIVE and
NUMERIC keys. Change to:

mysql_fetch_array($qstatus,MYSQL_ASSOC);

valid options are MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH
default = MYSQL_BOTH

also

mysql_fetch_assoc($qstatus); // for associative keys only

Norm
May 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
5
by: New_aspect | last post by:
Hello, Aspect oriented Software development seems to be expanding in the popular vision of developers, with more and more IDE 'add-ons' and even more specialized tools,Jboss etc. I've seen more...
92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
9
by: Dr. Know | last post by:
Greetings, I am running IIS on W2kServer and am trying to use ASP with some legacy X-Base tables. I cannot get the provider to connect. Relevant code is: strProvider =...
28
by: Intermouse | last post by:
I have recently purchased an aspi wrapper control for vb. I haven't had much experience with hex and memory addresses and that's my problem. The piece of code that baffles me is: With ASPI1...
14
by: Charles Douglas Wehner | last post by:
If you go to http://www.netscape.com and search for Wehner, you will find my site. It will say http://wehner.org You click to preview, and find that my home page is too big for the preview...
12
by: > Adrian | last post by:
How do I fix a form on the screen using VS C# 2005. By "fixing" I mean that the user cannot move the form about on the display. Thanks, Adrian.
75
by: Masood | last post by:
Hi all, I've been reading this group new for a few weeks and it's quite intriguing the way it's so dysfunctional as a "society". Actually I was telling my brother about it - he's a sociology...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.