?
Solved

Slash between values question

Posted on 2010-08-25
5
Medium Priority
?
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

752 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