DysFunctional

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Akhenaten

    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($qi d,$userid)
    {
    include "db.inc.php ";
    $qstatus = mysql_query("SE LECT status FROM `questions` WHERE `qid` =
    '$qid'", $db);
    $query1 = mysql_fetch_arr ay($qstatus);
    // Query 2 and 1
    $result = "SELECT `a_uid` FROM `answers` WHERE `qid` = '$qid'";
    $query = mysql_query($re sult, $db);
    while ($item = mysql_fetch_arr ay($query)) {
    if (in_array($user id,$item)) {
    echo "<br><br>CA NT GO HERE";
    }
    if ( $query1['status'] = '1'){
    echo "<br><br><a href='apage.php ?qid=$qid'>GO HERE</a>";
    }
    }

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

  • Schraalhans Keukenmeester

    #2
    Re: DysFunctional

    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($qi d,$userid)
    {
    include "db.inc.php ";
    $qstatus = mysql_query("SE LECT status FROM `questions` WHERE `qid` =
    '$qid'", $db);
    $query1 = mysql_fetch_arr ay($qstatus);
    // Query 2 and 1
    $result = "SELECT `a_uid` FROM `answers` WHERE `qid` = '$qid'";
    $query = mysql_query($re sult, $db);
    while ($item = mysql_fetch_arr ay($query)) {
    if (in_array($user id,$item)) {
    echo "<br><br>CA NT 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($use r_id, $qid)
    {
    include 'db.inc.php'; // opens a connection

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

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

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

    while ($row = mysql_fetch_ass oc($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_hand ling("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.

    Comment

    • Norman Peelman

      #3
      Re: DysFunctional

      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($qi d,$userid)
      {
      include "db.inc.php ";
      $qstatus = mysql_query("SE LECT status FROM `questions` WHERE `qid` =
      '$qid'", $db);
      $query1 = mysql_fetch_arr ay($qstatus);
      // Query 2 and 1
      $result = "SELECT `a_uid` FROM `answers` WHERE `qid` = '$qid'";
      $query = mysql_query($re sult, $db);
      while ($item = mysql_fetch_arr ay($query)) {
      if (in_array($user id,$item)) {
      echo "<br><br>CA NT 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_arr ay(...) returns an array consisting both ASSOCIATIVE and
      NUMERIC keys. Change to:

      mysql_fetch_arr ay($qstatus,MYS QL_ASSOC);

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

      also

      mysql_fetch_ass oc($qstatus); // for associative keys only

      Norm

      Comment

      Working...