Guest huey4657 Posted July 14, 2006 Share Posted July 14, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/14584-implode-is-making-me-explode/ Share on other sites More sharing options...
brown2005 Posted July 14, 2006 Share Posted July 14, 2006 I I LIKE THE CATCHY HEADER, MADE ME LAUGH Quote Link to comment https://forums.phpfreaks.com/topic/14584-implode-is-making-me-explode/#findComment-57908 Share on other sites More sharing options...
CheesierAngel Posted July 14, 2006 Share Posted July 14, 2006 How are you fiilling your $Course array ?Or can you show us what's in the array ?[code]<?php echo "<pre>"; print_r($course); echo "</pre>";?>[/code]Would really help solving your probl. Quote Link to comment https://forums.phpfreaks.com/topic/14584-implode-is-making-me-explode/#findComment-57932 Share on other sites More sharing options...
Guest huey4657 Posted July 14, 2006 Share Posted July 14, 2006 Thanks please go to PHP HELP > Multiple checkbox / sql search problem topic the code is posted there Quote Link to comment https://forums.phpfreaks.com/topic/14584-implode-is-making-me-explode/#findComment-57933 Share on other sites More sharing options...
Barand Posted July 14, 2006 Share Posted July 14, 2006 [code]<?php$Course = array('blah1', 'blah2', 'blah3');$Coursearray = implode("','", $Course); // <-- inner quotes $SqlQueryA = "SELECT Course_Name FROM tblMeeting WHERE Course_Name IN ('$Coursearray')";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14584-implode-is-making-me-explode/#findComment-57935 Share on other sites More sharing options...
Oldiesmann Posted July 14, 2006 Share Posted July 14, 2006 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 Link to comment https://forums.phpfreaks.com/topic/14584-implode-is-making-me-explode/#findComment-57940 Share on other sites More sharing options...
Guest huey4657 Posted July 14, 2006 Share Posted July 14, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/14584-implode-is-making-me-explode/#findComment-57942 Share on other sites More sharing options...
Barand Posted July 14, 2006 Share Posted July 14, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/14584-implode-is-making-me-explode/#findComment-57946 Share on other sites More sharing options...
Guest huey4657 Posted July 14, 2006 Share Posted July 14, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/14584-implode-is-making-me-explode/#findComment-57954 Share on other sites More sharing options...
Barand Posted July 14, 2006 Share Posted July 14, 2006 Yes, but I wanted demonstrate that with an array of string values you need to implode with "','" and not just a comma Quote Link to comment https://forums.phpfreaks.com/topic/14584-implode-is-making-me-explode/#findComment-57966 Share on other sites More sharing options...
Guest huey4657 Posted July 14, 2006 Share Posted July 14, 2006 Thank-you Barand you beauty. Quote Link to comment https://forums.phpfreaks.com/topic/14584-implode-is-making-me-explode/#findComment-57971 Share on other sites More sharing options...
Oldiesmann Posted July 14, 2006 Share Posted July 14, 2006 [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 :) Quote Link to comment https://forums.phpfreaks.com/topic/14584-implode-is-making-me-explode/#findComment-57990 Share on other sites More sharing options...
Guest huey4657 Posted July 14, 2006 Share Posted July 14, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/14584-implode-is-making-me-explode/#findComment-58032 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.