Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Slash between values question

Posted on 2010-08-25
Medium Priority
373 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
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

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;
}
)
)
)
);
``````
0

LVL 40

Expert Comment

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

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

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'
)
)
)
);
``````
0

Author Closing Comment

ID: 33526760
THANK you :-)

Just what I needed!
0

## Featured Post

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
###### Suggested Courses
Course of the Month9 days, 19 hours left to enroll

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

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