Solved

Slash between values question

Posted on 2010-08-25
5
354 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:Richard Quadling
ID: 33526654
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:Richard Quadling
ID: 33526674
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
ID: 33526720
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:
Richard Quadling earned 500 total points
ID: 33526740
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
ID: 33526760
THANK you :-)

Just what I needed!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 look for a specific file type in a local or remote server directory using PHP.

685 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