Solved

Populating A Div

Posted on 2013-05-12
4
253 Views
Last Modified: 2013-05-13
I have a switch and I want to populate a div based on the case.  However; I can't seem to get the syntax correct.

<?php switch ($Alpha)
					{
					case "1":
						echo "<div id="BigName">Number 1</div>";
						echo "<table>";
 						echo "<tr>";
 						echo "<td>Row 1, cell 1</td>";
 						echo "<td>Row 1, cell 2</td>";
 						echo "</tr>";
						echo "<tr>";
 						echo "<td>Row 2, cell 1</td>";
 						echo "<td>Row 2, cell 2</td>";
 						echo "</tr>";
 						echo "</table>"; 
						break;

Open in new window


echo "<div id="BigName">Number 1</div>";
0
Comment
Question by:DS928
4 Comments
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39160424
If you have a list of integer values (1, 2, 3 etc) you don't have to use quotes. In addition, you forget to close the switch structure with a curling bracket:
 
<?php switch ($Alpha)
					{
					case 1:
						echo "<div id="BigName">Number 1</div>";
						echo "<table>";
 						echo "<tr>";
 						echo "<td>Row 1, cell 1</td>";
 						echo "<td>Row 1, cell 2</td>";
 						echo "</tr>";
						echo "<tr>";
 						echo "<td>Row 2, cell 1</td>";
 						echo "<td>Row 2, cell 2</td>";
 						echo "</tr>";
 						echo "</table>"; 
						break;
}
                                  

Open in new window


I don't understand what the last line of your question means, but I don't see other syntax errors...
0
 
LVL 7

Assisted Solution

by:kshna
kshna earned 167 total points
ID: 39160636
you need to escape dobule quotes by backslash

for example:
echo "helo \"World\" done " ;
0
 
LVL 4

Assisted Solution

by:ramyajanarthanan
ramyajanarthanan earned 166 total points
ID: 39160953
Why don't you render your file

<?php
switch ($Alpha) {
            case '1' :
             include ('filename.php');
            break;
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 167 total points
ID: 39161272
For the switch/case behavior, please see the teaching example here:
http://www.laprbass.com/RAY_switch_example.php?i=x

<?php // RAY_switch_example.php
error_reporting(E_ALL);
echo "<pre>";


// DEMONSTRATE THE USE OF SWITCH / CASE CONDITIONAL LOGIC
// MAN PAGE: http://php.net/manual/en/control-structures.switch.php
// NOTE: "The switch statement is similar to a series of IF statements ON THE SAME EXPRESSION."
// TRY THIS SCRIPT WITH URL: /RAY_switch_example.php?i=x


// THE DEFAULT QUERY STRING
$sql = "SELECT thing FROM table WHERE 1=1";

// IF ANYTHING IN THE URL ARGUMENT
if (!empty($_GET["i"]))
{
    // NORMALIZE THE URL ARGUMENT WITH THESE RULES
    $i = (string)$_GET['i'];
    $i = trim($i);
    $i = substr($i,0,1);
    $i = strtoupper($i);

    // SWITCH ON THE NORMALIZED VARIABLE
    switch ($i)
    {
        case "A" :
        case "B" : echo "YOU ENTERED EITHER 'A' OR 'B' ";
                   $sql = "SELECT thing FROM table WHERE thing = 'AAA' OR thing = 'BBB'";
                   break;

        case "C" : echo "YOU ENTERED 'C' ";
                   $sql = "SELECT thing FROM table WHERE thing = 'CCC'";
                   break;

        default  : echo "YOU ENTERED NONE OF A, B OR C ";
    }

    echo PHP_EOL;
    echo "THE CHOSEN QUERY STRING SAYS: $sql";
}

// CREATE THE FORM FOR INPUT (OR JUST USE THE URL)
$form = <<<FORM
<form>
ENTER 'A' 'B' OR 'C'
<input name="i" autocomplete="off" />
<input type="submit" />
</form>
FORM;
echo $form;

Open in new window

For your switch/case implementation, you may find that the HEREDOC syntax is easier to get right.  It helps you avoid all the fiddly punctuation that is necessary when you have quotes embedded in quotes!  Don't worry about the warning on the man page - just heed it.

<?php 
error_reporting(E_ALL);

// USE SWITCH-CASE TO SET UP THE CORRECT HTML STRING
switch ($Alpha)
{
    // FOR HTML STRINGS, USE HEREDOC NOTATION 
    case 1:
    $html = <<<EOD
<div id="BigName">Number 1</div>
<table>
<tr>
<td>Row 1, cell 1</td>
<td>Row 1, cell 2</td>
</tr>
<tr>
<td>Row 2, cell 1</td>
<td>Row 2, cell 2</td>
</tr>
</table>
EOD;
    break;
    
    // FOR SIMPLE ASSIGNMENTS NO HEREDOC IS NEEDED
    default: 
    $html = NULL;
    break;
}

// WRITE THE PREPARED HTML STRING TO THE BROWSER
echo $html;

Open in new window

To get a good foundation in the syntax and functionality of PHP you might consider getting this book.  A month of study will put you two years ahead of trial-and-error, I promise.
http://www.amazon.com/PHP-MySQL-Web-Development-Edition/dp/0672329166/


Best regards, ~Ray
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

744 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

10 Experts available now in Live!

Get 1:1 Help Now