Jump to content

Recommended Posts

<?php 
include('config.php');
//agarra productos a la lista.

$usuario=$_GET['id'];
$producto=$_GET['prod'];
$sql= "SELECT lista FROM usuarios WHERE id=$usuario";
$consulta = mysql_query($sql);
$row = mysql_fetch_assoc($consulta);

if($lista = $row['lista']){
  $lista = explode(",",$lista);
} else {
  $lista = array();
}

if(!in_array($producto,$lista)){
$lista[] = $producto;
$lista = implode(",",$lista);
mysql_query("UPDATE usuarios SET lista = '$lista' WHERE id=$usuario");
echo "Producto agregado";
}else{
echo "Ya existe";
}
?>

 

what this code does is to add items to an array, works pefectly, now i´m wondering un a way to limit the amount of items on the array and to add the posibility to eliminate a "item" from the array

 

regards

Link to comment
https://forums.phpfreaks.com/topic/182030-edit-an-array/
Share on other sites

What do yo umean unlimit the amount of items on the array? How are they limited?

 

you can remove elements from an array like so

<?php
$arr = array();

for ($i = 0; $i < 10; $i++){
$arr[] = $i;
}

$key = array_keys($arr, 3);//gets an array of the keys with the value of 0
unset($arr[$key[0]]);//this will unset the first entry in the array with the value we searched for
print_r($arr);
?>

 

This outputs

Array ( [0] => 0 [1] => 1 [2] => 2 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 )

Link to comment
https://forums.phpfreaks.com/topic/182030-edit-an-array/#findComment-960144
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.