Jump to content

[SOLVED] Issue with multiple arrays and select


seby

Recommended Posts

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?

Link to comment
Share on other sites

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'];
}

 

Link to comment
Share on other sites

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!

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.