Julian Posted February 5, 2007 Share Posted February 5, 2007 Hi Guys Here's what I have now: <?php $sql = "SELECT id_tour, tour FROM tours WHERE id_packages = $colname_front ORDER BY id_tour"; $res = mysql_query($sql) or die(mysql_error()); echo '<table width="339" border="0" cellspacing="0" cellpadding="5">'; $count = 0; while (list($id_tour, $tour) = mysql_fetch_row($res)) { if ($count % 2 == 0) echo '<tr>'; echo "<td><td><a href='tourdetail.php?id_tour=$id_tour'>$tour</a></td>"; $count++; if ($count % 2 == 0) echo '</tr>'; } // if we got to the end but haven't closed the row if ($count % 2 != 0) echo '</tr>'; echo '</table>'; ?> The problem I have is that id_packages on turs table is an array (1,2,3,4), and when I try to display the info, only works with the first number on the array. I think I have to explode the array first, but what I don't know is how to integrate the exploded variable to the script. Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/37165-solved-help-with-an-array/ Share on other sites More sharing options...
Xinil Posted February 5, 2007 Share Posted February 5, 2007 If you're trying to explode a string into an array, you do this: $myString = "1,2,3,4"; $newArray = explode(",",$myString); foreach ($newArray as $key => $value) { echo $value, ' - '; } Hope that makes sense. Link to comment https://forums.phpfreaks.com/topic/37165-solved-help-with-an-array/#findComment-177516 Share on other sites More sharing options...
Psycho Posted February 5, 2007 Share Posted February 5, 2007 I think you are just making the problem more complex than it needs to be. Plus, there is some problems with the code in general (two "<td>" right next to each other?). Using list() to put the values into explicit variables is just creating additional overhead IMHO. Will this work for you: <?php $sql = "SELECT id_tour, tour FROM tours WHERE id_packages = $colname_front ORDER BY id_tour"; $res = mysql_query($sql) or die(mysql_error()); echo '<table width="339" border="0" cellspacing="0" cellpadding="5">'; $count = 0; while ($row = mysql_fetch_row($res)) { if ($count % 2 == 0) echo '<tr>'; echo "<td><a href=\"tourdetail.php?id_tour={$row['id_tour']}\">{$row['tour'}</a></td>"; $count++; if ($count % 2 == 0) echo '</tr>'; } // if we got to the end but haven't closed the row if ($count % 2 != 0) echo '</tr>'; echo '</table>'; ?> But, I'm a bit confused by your statement "id_packages on turs table is an array (1,2,3,4)". Acording to your query, id_packages is a column in the table. Are you saying that in "WHERE id_packages = $colname_front" that $colname_front is an array of values in the format (1,2,3,4). If so then it should read "WHERE id_packages IN $colname_front" Link to comment https://forums.phpfreaks.com/topic/37165-solved-help-with-an-array/#findComment-177525 Share on other sites More sharing options...
Julian Posted February 5, 2007 Author Share Posted February 5, 2007 Thanks mjdamato Here's the thing: id_packages on Tours table is an array (1,2,3,4). Because tours can be shown at different Packages. The page displays Package information and optional tours for this package ($colname_front). What I'm trying to do here is to show the tours that are attached to this package in particular. So I have to explode the id_packages in Table tours and match the package on screen. I did achieve this with the original script. But the problem was that the script doesn't explode the array, so, it worked only with the first number on the array. Thanks again Link to comment https://forums.phpfreaks.com/topic/37165-solved-help-with-an-array/#findComment-177566 Share on other sites More sharing options...
Psycho Posted February 5, 2007 Share Posted February 5, 2007 OK, something is not right here. You state, again, "id_packages on Tours table is an array (1,2,3,4)". But, based on your query id_packages is a column in the table. Are you saying that the values in that column are an array? (If so, is a true array [serialized] or just comma separated values) If that is the case, i think I am understanding the problem. I *think* what you are saying is that you may be searching for tours where the id_packages contains a particular value. And, some records may have "(1,2,4)", or "(2,3)", or "(1,4)". Well, you *should* make the id_packages a separate table if that is the case, but if you don't want to do that, you could change your search to this: "WHERE id_packages LIKE '%".$colname_front."'" If that isn't what you are doing then I suggest you be very specific. Posibly give some examples of some records and exactly what you would be searching for. Link to comment https://forums.phpfreaks.com/topic/37165-solved-help-with-an-array/#findComment-177695 Share on other sites More sharing options...
Julian Posted February 5, 2007 Author Share Posted February 5, 2007 Thanks for the help mjdamato I'll try to explain myself better: Table Packages: id_packages | Name 1 | Northwest Beaches 2 | Mountain ******************************** Table Tours id_tours | id_packages | Tour 1 | 1,3 | Scuba Diving 2 | 1,2,3 | Hiking Ok, those are an example of the tables. With this script: <?php $sql = "SELECT id_tour, tour FROM tours WHERE id_packages = $colname_front ORDER BY id_tour"; $res = mysql_query($sql) or die(mysql_error()); echo '<table width="339" border="0" cellspacing="0" cellpadding="5">'; $count = 0; while (list($id_tour, $tour) = mysql_fetch_row($res)) { if ($count % 2 == 0) echo '<tr>'; echo "<td><a href='tourdetail.php?id_tour=$id_tour'>$tour</a></td>"; $count++; if ($count % 2 == 0) echo '</tr>'; } // if we got to the end but haven't closed the row if ($count % 2 != 0) echo '</tr>'; echo '</table>'; ?> I'm able to show only the tours associated to the packages. But this only work with the first number of the array. You were right here: "I *think* what you are saying is that you may be searching for tours where the id_packages contains a particular value. And, some records may have "(1,2,4)", or "(2,3)", or "(1,4)"." The question is how to do that. Explode id_packages and then return the associated tours. FYI: $colname_front is a value I passed from the previous page to get the info of that Package. Link to comment https://forums.phpfreaks.com/topic/37165-solved-help-with-an-array/#findComment-177716 Share on other sites More sharing options...
Psycho Posted February 5, 2007 Share Posted February 5, 2007 $sql = "SELECT id_tour, tour FROM tours WHERE id_packages LIKE '%{$colname_front}%' ORDER BY id_tour"; You really should put the package_id's in a separate table. But, this should work with your currect structure as long as your id_packages values are single digits. Link to comment https://forums.phpfreaks.com/topic/37165-solved-help-with-an-array/#findComment-177748 Share on other sites More sharing options...
Julian Posted February 5, 2007 Author Share Posted February 5, 2007 Thanks mjdamato You got it right. Link to comment https://forums.phpfreaks.com/topic/37165-solved-help-with-an-array/#findComment-177751 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.