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

DysFunctional

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.