Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Populating A Div

Posted on 2013-05-12
4
Medium Priority
?
261 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 668 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 664 total points
ID: 39160953
Why don't you render your file

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

Accepted Solution

by:
Ray Paseur earned 668 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
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…

722 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