Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 02-08-2006, 02:40 AM Array Help!!
Skilled Talker

Posts: 70
Trades: 0
I'm working on a page which is supposed to add, delete and search for elements in an array. However I'm not sure how to use the conditional statements around the arrays. Here's what I have so far:

PHP Code:
foreach($_POST as $key => $value) {
$
$key $value;
}
if(isset(
$cmd_add)) { // Checks if the "Add Element" button is pressed
// Array creation code goes here ::HELP::
// array_push() goes here?
}
elseif(isset(
$cmd_delete)) {
// Search for array element ::HELP:: array_search() ?
// unset($array[$i]); ?????
elseif(isset($cmd_search)) {
// Array search code goes here ::HELP::
// Result message goes here 
Then, to display the current elements on the page, would I do this?

PHP Code:
for($x 0$x 5$x++) {
echo 
$array[$x];

Also, would i need to use sessions in the case that a user wishes to add/delete more than one element?

my uploaded copy is at: http://203.206.125.189/ipt/arrays.php

Last edited by cerebro89; 02-08-2006 at 02:43 AM..
cerebro89 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-08-2006, 04:40 AM Re: Array Help!!
digitalfields's Avatar
Skilled Talker

Posts: 50
Trades: 0
To display the elements you need to change it slightly.

PHP Code:
for($x 0$x count($array); $x++) {
echo 
$array[$x];

That'll display all elements, regardless of size.

If you want to add and delete elements to the array,youll have to store the array in a session to stop it being lost everytime the page is loaded.

So after the original code you posted which checks what should be carried out (add,delete,search) save the array into a session
PHP Code:
$_SESSION['myarray'] = $array
Then at the start of the page
PHP Code:
if(isset($_SESSION['myarray']))
$array $_SESSION['myarray'];
else
$array = array(); // first time user has loaded page 
__________________
Digital Fields - Web Design

Please login or register to view this content. Registration is FREE
digitalfields is offline
Reply With Quote
View Public Profile Visit digitalfields's homepage!
 
Old 02-08-2006, 05:20 AM Re: Array Help!!
Skilled Talker

Posts: 70
Trades: 0
would this be the best way of checking if array elements exist?

PHP Code:
if(isset($cmd_add)) {
$array array_push($array,$txt_add);
}
if(isset(
$cmd_delete)) {
// I'M STUCK HERE AS I'M UNSURE OF THE SYNTAX!!!!
}
if(isset(
$cmd_search)) {
if(
in_array($txt_search,$array)) {
$result "This number is present in the array."
} else {
$result "This number is not present in the array."
}

cerebro89 is offline
Reply With Quote
View Public Profile
 
Old 02-08-2006, 05:36 AM Re: Array Help!!
digitalfields's Avatar
Skilled Talker

Posts: 50
Trades: 0
Have a look at array_search.
__________________
Digital Fields - Web Design

Please login or register to view this content. Registration is FREE
digitalfields is offline
Reply With Quote
View Public Profile Visit digitalfields's homepage!
 
Reply     « Reply to Array Help!!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.42918 seconds with 12 queries