Solved

Populating A Div

Posted on 2013-05-12
4
258 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 31

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 109

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn how to dynamically set the form action using jQuery.

820 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