Solved

Populating A Div

Posted on 2013-05-12
4
260 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
[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
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 110

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

630 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