Jump to content

Implode is making me explode!!


Guest huey4657

Recommended Posts

Guest huey4657
Hi all,
Can someone help me with this function...

I used the implode function but the problem I am now getting is that after using implode I can only search the database and return a result when there is only one item stored in the array, when there is more than one item (Blah1,Blah2,Blah3,Blah4) in the array it returns 0 Rows. Here is the code...
[code]
<?php

  /* Program: Stats.php

    Desc:    Allows users to select the required
              variables to create statistics. */

  /* Function to start session */

  session_start();

?>

<html>

<head>

  <title>Horse Racing Stats</title>

</head>

<body topmargin    = "0"
      leftmargin  = "0"
      marginheight = "0"
      marginwidth  = "0">



  <table width      = "100%"
          border      = "0"
          cellpadding = "5"
          cellspacing = "0">

    <tr>

    <!-- Create page header -->

    <td height  = "10%"
        colspan = "3"
        align  = "center"
        valign  = "top">

      <font color = "gray"
            size  = "+2">

          <b>Selected statistics</b>

      </font>

    </td>

    </tr>

<?php

    /* Read file that stores connection variables */

    include("Lame.inc");

    /* Connect to server and database */

    $MakeCon = mysql_connect($hostserver, $username, $pass)
                or die ("Could not connect to server.");

    $ConnDB = mysql_select_db($db, $MakeCon)
                or die ("Could not connect to database.");

    $Coursearray = implode(",", $Course);

    $SqlQueryA = "SELECT Course_Name
                  FROM tblMeeting
                  WHERE Course_Name IN ('$Coursearray')";

    $SqlResA = mysql_query($SqlQueryA)
              or die ("Could not execute query");

    $NumRows = mysql_num_rows($SqlResA);

    /** This shows the inside of the array for testing purposes**/

    echo "$Coursearray\n\n";

    /** This shows how many records have been returned for testing purposes **/

    echo "$NumRows ROWS";

?>

</table></body></html>
[/code]

Here is an image of the result (that works) when only one item in the array:-
[img]http://[/img]
Here is an image of the result (that fails) when more that one item is in the array:-
[img][/img]
Please can someone help me... thanks

[attachment deleted by admin]
Link to comment
Share on other sites

[code]<?php
$Course = array('blah1', 'blah2', 'blah3');
$Coursearray = implode("','", $Course);      // <-- inner quotes

    $SqlQueryA = "SELECT Course_Name
                  FROM tblMeeting
                  WHERE Course_Name IN ('$Coursearray')";
?>
[/code]
Link to comment
Share on other sites

Almost there :)

[code]<?php
$Course = array('blah1', 'blah2', 'blah3');
$Coursearray = implode("','", $Course);      // <-- inner quotes

    $SqlQueryA = "SELECT Course_Name
                  FROM tblMeeting
                  WHERE Course_Name IN ($Coursearray)"; // <-- No quotes needed here...
?>
[/code]

Since the query statement is already in double quotes, you don't need single quotes around $Coursearray.
Link to comment
Share on other sites

Guest huey4657
Hi Guys,
The array comes from muliple checkbox selections from the user.
The inside of the array looks like this after implode (Blah1,Blah2,Blah3) when this is used in the SELECT query (SELECT Course_Name FROM tblMeeting WHERE Course_Name IN ('$Coursearray')). I think the problem is it is reading the array as one string?
Link to comment
Share on other sites

[quote author=Oldiesmann link=topic=100564.msg397037#msg397037 date=1152890124]
Almost there :)

[code]<?php
$Course = array('blah1', 'blah2', 'blah3');
$Coursearray = implode("','", $Course);      // <-- inner quotes

    $SqlQueryA = "SELECT Course_Name
                  FROM tblMeeting
                  WHERE Course_Name IN ($Coursearray)"; // <-- No quotes needed here...
?>
[/code]

Since the query statement is already in double quotes, you don't need single quotes around $Coursearray.
[/quote]

If you you remove those quotes the query becomes

....WHERE Course_Name IN ( blah1','blah2','blah3) ";

which is now clearly wrong
Link to comment
Share on other sites

Guest huey4657
Thats right mate But I did not create the array, the user creates the array from the multiple options they select. Here is the code:-
[code]
<?php

  /* Program: Stats.php

    Desc:    Allows users to select the required
              variables to create statistics. */

  /* Function to start session */

  session_start();

?>

<html>

<head>

  <title>Horse Racing Stats</title>

</head>

<body topmargin    = "0"
      leftmargin  = "0"
      marginheight = "0"
      marginwidth  = "0">



  <table width      = "100%"
          border      = "0"
          cellpadding = "5"
          cellspacing = "0">

    <tr>

    <!-- Create page header -->

    <td height  = "10%"
        colspan = "3"
        align  = "center"
        valign  = "top">

      <font color = "gray"
            size  = "+2">

          <b>Select statistics</b>

      </font>

    </td>

    </tr>

    <?php

    /* Read file that stores connection variables */

    include("Lame.inc");

    /* Connect to server and database */

    $MakeCon = mysql_connect($hostserver, $username, $pass)
                or die ("Could not connect to server.");

    $ConnDB = mysql_select_db($db, $MakeCon)
                or die ("Could not connect to database.");

    /* Create query that gets all race
        courses in alphabetical order */

    $SqlQuery1 = "SELECT Course_Name
                  FROM tblRaceCourse
                  ORDER BY Course_Name";

    /******* Irrelevant code cut here*******/

    /* Execute query */

    $SqlRes1 = mysql_query($SqlQuery1)
              or die ("Could not execute query");

      /******* Irrelevant code cut here*******/

    ?>

    <tr>

    <!-- Create table with blue bachground -->

    <td width  = "80%"
        rowspan = "2"
        align  = "left"
        valign  = "middle">

      <!-- Create form containing stats list -->

      <?php

      echo "<form action = 'CreateStats.php'
                  method = 'POST'>\n";

      echo "<table cellpadding = '5'
                    border      = '0'>\n";

      echo "<tr><td><font size = '2'><b>Course</b></font></td>";

      /* reset counter */

      $RowCnt1 = 0;

      /* For each category record fetched
          Create a checkbox */
                                           
      while($Row1 = mysql_fetch_array($SqlRes1))
             
            {

            /* Extract record into variables named
              after their database field name */

            extract($Row1);

            /* Create checkbox */

            echo "<td>

                  <input type  = 'checkbox'
                          name  = 'Course[]'
                          value = '$Course_Name'";

            /* Create checkbox label */

            echo "checked><font size = '1'>$Course_Name</font></td>\n";

            /* Increase count by one */
                           
            $RowCnt1++;

            /* Variable equals modulus
              of count and 9 */

            $NewLine1 = $RowCnt1 % 5;

            /* If modulus is equal to zero
              then create new table row,
              this is to stop horizontal
              scroll bar from appearing */

            if($NewLine1 == 0)

              {

              echo "</tr><tr><td></td>";

              }
                                               
            }

    /******* Irrelevant code cut here*******/

      echo "</tr></table>\n";

      /* Create submit form button */                           

      echo "<p>

              <input type  = 'submit'
                    value = 'Submit Course'>\n";

      echo "</form>\n";

      ?>

</body>

</html>
[/code]
Link to comment
Share on other sites

[quote author=Barand link=topic=100564.msg397043#msg397043 date=1152890441]
[quote author=Oldiesmann link=topic=100564.msg397037#msg397037 date=1152890124]
Almost there :)

[code]<?php
$Course = array('blah1', 'blah2', 'blah3');
$Coursearray = implode("','", $Course);      // <-- inner quotes

    $SqlQueryA = "SELECT Course_Name
                  FROM tblMeeting
                  WHERE Course_Name IN ($Coursearray)"; // <-- No quotes needed here...
?>
[/code]

Since the query statement is already in double quotes, you don't need single quotes around $Coursearray.
[/quote]

If you you remove those quotes the query becomes

....WHERE Course_Name IN ( blah1','blah2','blah3) ";

which is now clearly wrong
[/quote]

For some reason I was thinking that implode("', '") would put the ' at the beginning and end as well... Not trying to cause any problems :)
Link to comment
Share on other sites

Guest huey4657
Yes, no worries oldiesmann! you was just trying to help. right or wrong the more advice the better so we can learn while trying things out. thks
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.