seby Posted January 21, 2009 Share Posted January 21, 2009 Hi there. I'm quite new to php programming and I'm stucked atthis issue. using this select: SELECT * from postmeta WHERE post_id = $postid && meta_key LIKE 'movie-link%' will get me sets of links for current post. These sets are defined with 'movie-link%' keys and are always ending with number. So I have set of few links with 'movie-link1' keys, one link with 'movie-link2' key and so on. Using the 'movie-link%' will make it one-call-for-all, thats why I'm using it - db load. Using the next code, I fetch data and create array with indexes => links correctly associated (I hope?) while ( $row = mysql_fetch_array( $request ) ) { $index = str_replace("movie-link", "", $row[2]); $array = array($index => array($row[3])); } The problem is, that I get one array, containing all the links. Something like this, I think: Array ( [0] => Array ( [1] => http://localhost/mosofa/1 ) [1] => Array ( [1] => http://localhost/mosofa/2 ) [2] => Array ( [1] => http://localhost/mosofa/3 ) [3] => Array ( [2] => http://localhost/mosofa/22 ) [4] => Array ( [3] => http://localhost/mosofa/22 ) [5] => Array ( [3] => http://localhost/mosofa/223 ) [6] => Array ( [3] => http://localhost/mosofa/444 ) ) I guess, I need to create separate arrays for each set and then print them as separate ul lists. By now, with my experience, I'm only able to print them as one <ul> list and can't find a clue how to make this come true. Any ideas please? Quote Link to comment https://forums.phpfreaks.com/topic/141848-solved-issue-with-multiple-arrays-and-select/ Share on other sites More sharing options...
jjacquay712 Posted January 21, 2009 Share Posted January 21, 2009 while ( $row = mysql_fetch_array( $request ) ) { $index = str_replace("movie-link", "", $row[2]); $array = array($index => array($row[3])); } you dont need to create another array in the while loop, the fetch array function does it for you. if you wanted to echo the data from the database do this: while ( $row = mysql_fetch_array( $request ) ) { echo $row['whateverfieldyouwant']; } Quote Link to comment https://forums.phpfreaks.com/topic/141848-solved-issue-with-multiple-arrays-and-select/#findComment-742718 Share on other sites More sharing options...
sasa Posted January 21, 2009 Share Posted January 21, 2009 try while ( $row = mysql_fetch_array( $request ) ) { $index = str_replace("movie-link", "", $row[2]); $array[$index][] = $row[3]; } Quote Link to comment https://forums.phpfreaks.com/topic/141848-solved-issue-with-multiple-arrays-and-select/#findComment-742730 Share on other sites More sharing options...
seby Posted January 22, 2009 Author Share Posted January 22, 2009 Wow guys, thanks for prompt responses! I've figured out the whole thing thanks to sasa's $array[$index][] = $row[3]; I get the arrays just as I wanted: Array ( [1] => Array ( [0] => http://localhost/moviesofa/1 [1] => http://localhost/moviesofa/2 [2] => http://localhost/moviesofa/3 ) [2] => Array ( [0] => http://localhost/moviesofa/22 ) [3] => Array ( [0] => http://localhost/moviesofa/22 [1] => http://localhost/moviesofa/223 [2] => http://localhost/moviesofa/444 ) ) I'm not sure if this issue can be solved other ways, but this one works quite good for me. So finally, with 2 for loops, I'm able to print just what I needed. $sql = "SELECT * from wp_postmeta WHERE post_id = $postid && meta_key LIKE 'movie-link%'"; $request = mysql_query($sql); while ( $row = mysql_fetch_array( $request ) ) { $index = str_replace("movie-link", "", $row[2]); $array[$index][] = $row[3]; } $pocet =count($array); for( $i=1; $i<=$pocet ; $i++ ){ $pocet2 = count($array[$i]); echo "<ul>"; for ( $p=0; $p<$pocet2 ; $p++ ){ echo "<li>".$array[$i][$p]."</li>"; } echo "</ul>"; } Will output me any number of <ul> lists, with any number of <li> items. Thanks a lot again! Quote Link to comment https://forums.phpfreaks.com/topic/141848-solved-issue-with-multiple-arrays-and-select/#findComment-743058 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.