Jump to content

array unique won't work


gongpex
 Share

Recommended Posts

Hello everyone please see this code :

<?$data = array();$data[0] = "test";$data[1] = "price";$data[2] = "img";$data[3] = "img";echo(array_unique($data));?>

if I wrote code like on above it won't show anything, but if I using print_r it can displayed Q : Actually can I display it using echo ? So that the result shown like this :

testpriceimg
please answer Thanks
Link to comment
Share on other sites

array_unique() return an array. print_r() can display arrays. echo can't. you have to loop through the array to echo those elements.http://au.php.net/array_unique

Link to comment
Share on other sites

I think I need to the point, please see this image : unique.jpg code on above :

$count = count($_SESSION['buy']);  while($x<$count){$product['title'][$x] = $_SESSION['buy'][0];$product['price'][$x] = $_SESSION['buy'][1];$product['img'][$x] = $_SESSION['buy'][2];}

Q : Can I using array_unique to avoid duplicate array value like image on above? Q : If array_unique not possible, what I must do so that array value not duplicated ? please help me thanks

Edited by gong
Link to comment
Share on other sites

Yes, array_unique removes duplicate values. What I don't understand is why you're setting several values to the same values from $_SESSION. Why are you filling up the product array with the first 3 entries in $_SESSION? That's creating duplicate data.

Link to comment
Share on other sites

Thanks for reply,

Why are you filling up the product array with the first 3 entries in $_SESSION?
A : I think I need 3 entries to show product at user. I 'll to remove some entries of $_SESSION array, if I get stuck please help me Thanks
Link to comment
Share on other sites

This code just doesn't really make sense to me:

$count = count($_SESSION['buy']);while($x<$count){  $product['title'][$x] = $_SESSION['buy'][0];   $product['price'][$x] = $_SESSION['buy'][1];  $product['img'][$x] = $_SESSION['buy'][2];}

If $_SESSION['buy'] contains only those three items, and $count is 3, and assuming $x starts at 0, then you're going to end up with the same 3 values from $_SESSION['buy'] duplicated in $product['title'][0], $product['title'][1], and $product['title'][2]. I don't know why you need the same values in all 3 of those titles. You're not changing the indexes for $_SESSION['buy'], so it's always copying the values from $_SESSION['buy'][0], $_SESSION['buy'][1], and $_SESSION['buy'][2]. Those 3 values get copied into the other array more than once. I don't know what the point of that is, you're just duplicating the same 3 values multiple times. That loop is also missing an increment for $x, so it's never going to stop.

Link to comment
Share on other sites

Sorry, I forgot to post something here, please see this code :

<form method="post" action="buy.php">	    <input type="text" value="<?$id1 = $product_id?>" style="display:none" name="test" /></div>						 <div class="pr" style="height:15px"><b style="font-size:14px"><?echo"$product_name";?></b></div>						 <div class="pr"><input type="submit" value="BUY NOW" /></div></form>   //this is product 1<form method="post" action="buy.php">	    <input type="text" value="<?$id2 = $product_id2;?>" style="display:none" name="test" /></div>						 <div class="pr" style="height:15px"><b style="font-size:14px"><?echo"$product_name2";?></b></div>						 <div class="pr"><input type="submit" value="BUY NOW" /></div></form>   //this is product 2

When I click "BUY NOW" for every product (only once) it will shown like this : expect.png And if click "BUY NOW" twice or more , it will occurs like image on above. I will try your suggestion to remove $product['title'][$x], If I got stuck I will post here Thanks________________________________________________________________________________________ OFFQ : My friend told to me if better I learn java rather than PHP, he told me because in the future java is the best to build web, rather than PHP, is that correct ? please explain me (if not objection) Thanks again

Link to comment
Share on other sites

Hi, This to reply :

If $_SESSION['buy'] contains only those three items, and $count is 3, and assuming $x starts at 0, then you're going to end up with the same 3 values from $_SESSION['buy'] duplicated in $product['title'][0], $product['title'][1], and $product['title'][2]. I don't know why you need the same values in all 3 of those titles. You're not changing the indexes for $_SESSION['buy'], so it's always copying the values from $_SESSION['buy'][0], $_SESSION['buy'][1], and $_...
This is my fix code :
<?if($_SESSION['buy']){$count = count($_SESSION['buy']);for($x=0;$x<$count;$x++){$product_title = $_SESSION['buy'][$x][0];$product_price = $_SESSION['buy'][$x][1];$product_img = $_SESSION['buy'][$x][2];$product_id = $_SESSION['buy'][$x][3];$product_qty = $_SESSION['buy'][$x][4]; echo"$product_title<br />$product_price<br />$product_img<br />$product_qty<br /><br/>";}}else{echo"fail";}?>

I had try new code, this my code :

<?$product = array();$product[0] = $_SESSION['buy'][0];$product[1] = $_SESSION['buy'][1];$product[2] = $_SESSION['buy'][2];$product[3] = $_SESSION['buy'][3];$product[4] = $_SESSION['buy'][4];  $product = array_unique($product);  if($_SESSION['buy']){$count = count($_SESSION['buy']);for($x=0;$x<$count;$x++){  $product_title = $product[$x][0];  $product_price = $product[$x][1];  $product_img = $product[$x][2];echo $product_title."<br />".$product_price."<br />".$product_img."<br /><br />";}}else{echo"fail";} 

Now I put array inside array, so that array_unique can be work, but if I delete code :

$product = array_unique($product);

It would be back like image on above (duplicated), But if I using this code , it's only show one product,and the other product cannot be shown. + About new code : Q : What's mistake from my code? + About fix code : This is still duplicated Q : from both of this code, which code that possible to be use so that when user buy product , it would be not duplicated? please help me Thanks

Edited by gong
Link to comment
Share on other sites

your code seem confusin, but why not display the information in the cart "since thats what it seems like your making" by the session id of the person whos logged in? so in your db you should have a login table and the other other table with a column name user_id which will hold the session_id in it. #justmyopinion

Link to comment
Share on other sites

This: <input type="text" value="<?$id1 = $product_id?>" style="display:none" name="test" /> Is not going to print anything in the value. You can verify that by viewing the source code that PHP produces. The value will be empty.

I will try your suggestion to remove $product['title'][$x],
I did not suggest to remove that. I'm asking questions, I'm trying to understand your code, not suggesting things to remove. I'm also trying to help you understand what your code is doing, it sounds like you don't understand it.
Link to comment
Share on other sites

Hello everyone please see this code :
<?$data = array();$data[0] = "test";$data[1] = "price";$data[2] = "img";$data[3] = "img";echo(array_unique($data));?>

if I wrote code like on above it won't show anything, but if I using print_r it can displayed Q : Actually can I display it using echo ? So that the result shown like this : please answer Thanks

Try this <?$data = array();$data[0] = "test";$data[1] = "price";$data[2] = "img";$data[3] = "img"; $new_data = array_unique($data); foreach($new_data as $elements){ echo "<br>" . $elements; } ?>
Link to comment
Share on other sites

Hi all, Thanks for answer, maybe I need to upload my site on temporary server, then I will show it on you all, If I already upload my site , I will post here again. Thanks everyone__________________________________________________________________________________________

I'm also trying to help you understand what your code is doing, it sounds like you don't understand it.
OFFT : Please be patient to help me to understand. Thanks all for patience Edited by gong
Link to comment
Share on other sites

Hello everyone Please come to my site, This is my site : http://secondbook.onlinewebshop.net/ Please click "BUY NOW" for every products twice or more, This is my question Q : How to make so that it show product only once and the quantity of product would be increase when user click on "BUY NOW" more than once? Q2 : How to create so that when user click "DEL" it will delete only at the product appropriate with user intent? please help me Thanks________________________________________________________________________________________________________ Note : if you need something please tell me, I will be tried to provide it for you , thanks

Edited by gong
Link to comment
Share on other sites

there is many way to do that. when user click on buy button check in session array that product id is there or not. if it is there increase the quantity.when you store a product you can store product id and count in two elements of array $_SESSION[1]['productid']$_SESSION[1]['quantity'] same will be with delete.in delete button you will pass the product id and when you submit it, it will decrease the quantity once it reaches 0 you will move the entire product.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...