Solved

Slash between values question

Posted on 2010-08-25
5
362 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 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 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