Jump to content

Array Help


timmah1

Recommended Posts

Is there any way to put an "if" statement in an array?

 

I'm building links, and if the member is a certain type, I want it to display certain links, and if their a different member type, to show a different link

 

Example:

There are two types of members, a buyer and a seller.

If you are a seller, you'll get the links "List" and "My Stuff"

If you are a buyer, you won't have those links.

 

Here is my array code, but I need to specify somewhere in there the account "type"

$data = array(
	'<p><a href="logout.php">Logout</a></p>
	 <p><a href="member.php?change=password">Change Password</a></p>
	 <p><a href="member.php?change=email">Change Email</a></p>
	 <p><a href="member.php?change=account">Close account</a></p>',

	'<p>Welcome to the members only page '.$_SESSION['s_name'].'!</p>
	<p>You are logged in as '.$_SESSION['s_username'].' from the IP address '.$_SERVER['REMOTE_ADDR'].'</p>',

	);

 

Thanks in advance

Link to comment
Share on other sites

You can use an if statement to decide what is to be displayed from an array, bt no, you put a statement within an array.

 

Your question is still a little vague however, how do you determin what array elements are displayed for what user?

 

One easy method would be to use a mutlidimensional array.....

 

<?php

  $data = array(
    'members' => array('a','b','c'),
    'buyers' => array('g','h','i'),
    'seller' => array('x','y','x')
  );

  $membertype = $_SESSION['membertype'];

  foreach($data[$membertype] as $val) {
    echo $val;
  }

?>

 

but really, its hard to tell what you want to do from your post.

Link to comment
Share on other sites

I have a field in the database name 'type', it either has a value of buyer or seller.

 

When you login, and your a seller, I want their menu to look like this:

 

My Account

My Items

List Item

Browse

Search

Change Email

Change Password

Logout

Close Account

 

If your a buyer, I want their menu to look like this:

My Account

Browse

Search

Change Email

Change Password

Logout

Close Account

 

I hope that explains it a little better

 

This is my whole code for the 'menu.php'

<?php session_start(); 

if($_GET['set'] > 0)
{

if($_SESSION['whattoclose'] == NULL)
{

	$_SESSION['whattoclose'] = $_GET['set'];

} else {

	$_SESSION['whattoclose'] = $_SESSION['whattoclose'] . '|' . $_GET['set'];

}

}

if($_GET['undo'] > 0)
{

if(!empty($_SESSION['whattoclose'])){

	$_SESSION['whattoclose'] = str_replace("|".$_GET['undo'] , "" , $_SESSION['whattoclose']);
	$_SESSION['whattoclose'] = str_replace($_GET['undo'] , "" , $_SESSION['whattoclose']);

}

}

function showTitle($id)
{

$title = array(
	"Navigation",
	"Login information",
	);

$data = $_SESSION['whattoclose'];

$not = explode( "|" , $data );
if(!in_array( $id , $not ))
{

	echo '<a href="member.php?set='.$id.'">close</a> - ';
	echo $title[$id-1];

} else {

	echo '<a href="member.php?undo='.$id.'">open</a> - ';
	echo $title[$id-1];

}		

}

function checkIt($id)
{

$data = array(
	'<p><a href="logout.php">Logout</a></p>
	 <p><a href="member.php?change=password">Change Password</a></p>
	 <p><a href="member.php?change=email">Change Email</a></p>
	 <p><a href="member.php?change=account">Close account</a></p>',

	'<p>Welcome to the members only page '.$_SESSION['s_name'].'!</p>
	<p>You are logged in as '.$_SESSION['s_username'].' from the IP address '.$_SERVER['REMOTE_ADDR'].'</p>',

	);

$str = $_SESSION['whattoclose'];

$not = explode( "|" , $str );
if(!in_array( $id , $not ))
{

	echo '<div class="table">';
	echo $data[$id-1];
	echo '</div>';

}

}

?>

<div class="header">
<? showTitle(1) ?>
</div>
<? checkIt(1) ?>
<br>
<div class="header">
<? showTitle(2) ?>
</div>
<? checkIt(2) ?>

Link to comment
Share on other sites

ok, I tried the multidimensional array, and I'm received an error on line 76 which is

 

}

Here is the error I get

Parse error: syntax error, unexpected ',' in /home/public_html/CORE/menus.php on line 76

 

Here is my whole code, with the multidimensional array

<?php session_start(); 

if($_GET['set'] > 0)
{

if($_SESSION['whattoclose'] == NULL)
{

	$_SESSION['whattoclose'] = $_GET['set'];

} else {

	$_SESSION['whattoclose'] = $_SESSION['whattoclose'] . '|' . $_GET['set'];

}

}

if($_GET['undo'] > 0)
{

if(!empty($_SESSION['whattoclose'])){

	$_SESSION['whattoclose'] = str_replace("|".$_GET['undo'] , "" , $_SESSION['whattoclose']);
	$_SESSION['whattoclose'] = str_replace($_GET['undo'] , "" , $_SESSION['whattoclose']);

}

}

function showTitle($id)
{

$title = array(
	"Navigation",
	"Login information",
	);

$data = $_SESSION['whattoclose'];

$not = explode( "|" , $data );
if(!in_array( $id , $not ))
{

	echo '<a href="member.php?set='.$id.'">close</a> - ';
	echo $title[$id-1];

} else {

	echo '<a href="member.php?undo='.$id.'">open</a> - ';
	echo $title[$id-1];

}		

}

function checkIt($id)
{

$data = array(

 // $data = array(
    //'members' => array('a','b','c'),
    'buyer' => array('your a buyer'),
    'seller' => array('your a seller')
  );

  $type = $_SESSION['s_name'];

  foreach($data[$type] as $val) {
    echo $val;
  }


	'<p>Welcome to the members only page '.$_SESSION['s_name'].'!</p>
	<p>You are logged in as '.$_SESSION['s_username'].' from the IP address '.$_SERVER['REMOTE_ADDR'].'</p>',

	);

$str = $_SESSION['whattoclose'];

$not = explode( "|" , $str );
if(!in_array( $id , $not ))
{

	echo '<div class="table">';
	echo $data[$id-1];
	echo '</div>';

}

}

?>

<div class="header">
<? showTitle(1) ?>
</div>
<? checkIt(1) ?>
<br>
<div class="header">
<? showTitle(2) ?>
</div>
<? checkIt(2) ?>

Link to comment
Share on other sites

Thank you thorpe, once again, for your guidance, I got the thing to work correctly.

 

My next question is this:

 

Why, now, does the links show up twice?

Here is the code

$data = array(
'seller' => array('	<p><a href="member.php">My Account</a></p>
					<p><a href="main.php?cid=list">List Items</a></p>
					<p><a href="main.php?cid=myitems">My Items</a></p>
					<p><a href="member.php?change=password">Change Password</a></p>
	 				<p><a href="member.php?change=email">Change Email</a></p>
	 				<p><a href="main.php?cid=logout">Logout</a></p>
					<p></p>
	 				<p><a href="member.php?change=account">Close account</a></p>'),

'buyer' => array('	<p><a href="member.php">My Account</a></p>
					<p><a href="member.php?change=password">Change Password</a></p>
					<p><a href="member.php?change=email">Change Email</a></p>
	 				<p><a href="main.php?cid=logout">Logout</a></p>
					<p></p>
	 				<p><a href="member.php?change=account">Close account</a></p>'),
    //buyer => array(<p><a href="member.php?change=password">Buyer</a></p>'),
'<p>Welcome to the members only page '.$_SESSION['s_name'].'!</p>
<p>You are logged in as '.$_SESSION['s_username'].' from the IP address '.$_SERVER['REMOTE_ADDR'].'</p>',
    
  );
  $type = $_SESSION['s_type'];

  foreach($data[$type] as $val) {
    echo $val;
  }

 

And what shows up is this

Main Links
close - Navigation
My Account
Change Password
Change Email
Logout
Close account

Welcome to the members only page!
You are logged in as cheezy from the IP address xx.xxx.xx.xxx

close - Login information

My Account
Change Password
Change Email
Logout
Close account

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.