Jump to content

every other number


MikeDXUNL

Recommended Posts

Searched google, only to find no luck; only something on PHP.net about array arguments or something

 

and no luck on here searching for 'even number picking'

 

If I have an array, say

 

[1] Array (

    [0] => This is Item 1

    [1] => This is Item 2 lol

    [2] => This is Item 3 haha

    [3] => This will be Item 4

    [4] => and finally, this is Item 5

)

 

 

Can someone point me where I can find how to get only:

$array[1][0]

$array[1][2]

$array[1][4]

 

and so on, out?

Link to comment
https://forums.phpfreaks.com/topic/109166-every-other-number/
Share on other sites

right.

 

$x = 0;
while ($array[1][$x]) {
  //code here
  $x = $x + 2;
}

 

or a slightly different method:

 

foreach($array[1] as $key => $val) {
   if (($key % 2) == 0) {
      // this position is even numbered
   } else {
     // this position is odd numbered..don't need the 'else' if you don't need to do something
     // special if it's odd
   }
}

 

Link to comment
https://forums.phpfreaks.com/topic/109166-every-other-number/#findComment-559978
Share on other sites

alright thanks! I chose the second method. but when inserting values into my DB there is another problem -_-

 

 

<?php


$k = 1;
$i = 0;
$max = count($achname[1]);



while($i <= $max-1)
  {
  	
  	
  	$in_achname = mysql_real_escape_string($achname[$k][$i]);
  	$in_achdesc = mysql_real_escape_string($achdesc[$k][$i]);
  	
  	
  	foreach($achimg[1] as $key => $val) {
   if (($key % 2) == 0) {
      $in_achimg = mysql_real_escape_string($achimg[1][$key]);
      
      
         mysql_query("INSERT INTO achievements (gameid, achname, achdesc, achimg) VALUES ('1', '$in_achname', '$in_achdesc', '$in_achimg')") or die(mysql_error());

      
   } else {
     // this position is odd numbered..don't need the 'else' if you don't need to do something
     // special if it's odd
   }

  	
}	





  $i++;
  }

?>

 

 

 

Looks like this in the DB:

+-------------------------------------------------------------------------------------------+

| achid | gameid | achname  | achdesc | achimg                                                              |

---------------------------------------------------------------------------------------------

|  1    |      1    | The Mas... | Destr...  | http://tiles.xbox.com/tiles/Ic/YE/0ICLiGJhbC9GCBoD...|

|  2    |      1    | The Mas... | Destr...  | http://tiles.xbox.com/tiles/LR/zN/1oCLiGJhbC83CxoD.. |

|  3    |      1    | The Mas... | Destr...  | http://tiles.xbox.com/tiles/Rw/+P/0oCLiGJhbC8xCxoD..|

 

 

 

the `achimg` acutally changes but the achname and achdesc change

Link to comment
https://forums.phpfreaks.com/topic/109166-every-other-number/#findComment-559997
Share on other sites

EDIT: you edited your post ahah

 

just checked my code, edited it but still same output

 

foreach($achimg[1] as $key => $val) {

 

  $in_achname = mysql_real_escape_string($achname[$k][$i]);

    $in_achdesc = mysql_real_escape_string($achdesc[$k][$i]);

 

  if (($key % 2) == 0) {

      $in_achimg = mysql_real_escape_string($achimg[1][$key]);

Link to comment
https://forums.phpfreaks.com/topic/109166-every-other-number/#findComment-560001
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.