Solved

Slash between values question

Posted on 2010-08-25
5
346 Views
Last Modified: 2012-05-10
Hello experts!

I have 10 columns in my database called table_nightclub_1, table_nightclub_2, table_nightclub_3, table_nightclub_4 and so on. Datatype = enum('Y', 'N', '').

Y if the table is booked - N if not.

How can I echo '1 / 2 / 3' if table 1, 2 and 3 are booked? And only '1' if table 1 is booked? '8 / 9' if table 8 and 9 are booked.

I can't find a smart way to make a slash between the values:-/ Hope someone can help me!

Thanks in advance!
0
Comment
Question by:sittinDuck
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
Try this. Uses closures and array functions.Outputs ...1 / 4 / 7 / 10
<?php
// Assuming data coming from database is in 1 row.
$data = array
	(
	'table_nightclub_1' => 'Y',
	'table_nightclub_2' => 'N',
	'table_nightclub_3' => 'N',
	'table_nightclub_4' => 'Y',
	'table_nightclub_5' => 'N',
	'table_nightclub_6' => 'N',
	'table_nightclub_7' => 'Y',
	'table_nightclub_8' => 'N',
	'table_nightclub_9' => 'N',
	'table_nightclub_10' => 'Y',
	);

echo
	implode // Step 4 : Implode them.
		(
		' / ',
		array_map // Step 3 : Drop the textual part of the keys.
			(
			function($value)
				{
				return substr($value, strlen('table_nightclub_'));
				},
			array_keys // Step 2 : Get the keys from the remaining 'Y's.
				(
				array_filter // Step 1 : Remove 'N's from array.
					(
					$data,
					function($value)
						{
						return 'N' != $value;
						}
					)
				)
			)
		);

Open in new window

0
 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
Another option would be to do it in SQL ...


SELECT
      CASE WHEN table_nightclub_1 = 'Y' THEN '1 / ' ELSE '' END +
      CASE WHEN table_nightclub_2 = 'Y' THEN '2 / ' ELSE '' END +
      CASE WHEN table_nightclub_3 = 'Y' THEN '3 / ' ELSE '' END +
      CASE WHEN table_nightclub_4 = 'Y' THEN '4 / ' ELSE '' END +
      CASE WHEN table_nightclub_5 = 'Y' THEN '5 / ' ELSE '' END +
      CASE WHEN table_nightclub_6 = 'Y' THEN '6 / ' ELSE '' END +
      CASE WHEN table_nightclub_7 = 'Y' THEN '7 / ' ELSE '' END +
      CASE WHEN table_nightclub_8 = 'Y' THEN '8 / ' ELSE '' END +
      CASE WHEN table_nightclub_9 = 'Y' THEN '9 / ' ELSE '' END +
      CASE WHEN table_nightclub_10 = 'Y' THEN '10' ELSE '' END
            AS Booked
FROM
      table

and in PHP

$Booked = trim($data['Booked'], ' /');

sort of thing.
0
 

Author Comment

by:sittinDuck
Comment Utility
There was an error with the first solution

Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in /home/tabelreservation.dk/wwwroot/test.php on line 23
0
 
LVL 40

Accepted Solution

by:
RQuadling earned 500 total points
Comment Utility
You're not on V5.3?

Old style equivalent.
<?php
// Assuming data coming from database is in 1 row.
$data = array
	(
	'table_nightclub_1' => 'Y',
	'table_nightclub_2' => 'N',
	'table_nightclub_3' => 'N',
	'table_nightclub_4' => 'Y',
	'table_nightclub_5' => 'N',
	'table_nightclub_6' => 'N',
	'table_nightclub_7' => 'Y',
	'table_nightclub_8' => 'N',
	'table_nightclub_9' => 'N',
	'table_nightclub_10' => 'Y',
	);

function Step1($value)
	{
	return 'N' != $value;
	}

function Step3($value)
	{
	return substr($value, strlen('table_nightclub_'));
	}

echo
	implode // Step 4 : Implode them.
		(
		' / ',
		array_map // Step 3 : Drop the textual part of the keys.
			(
			'Step3',
			array_keys // Step 2 : Get the keys from the remaining 'Y's.
				(
				array_filter // Step 1 : Remove 'N's from array.
					(
					$data,
					'Step1'
					)
				)
			)
		);

Open in new window

0
 

Author Closing Comment

by:sittinDuck
Comment Utility
THANK you :-)

Just what I needed!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now