Improve company productivity with a Business Account.Sign Up

x
?
Solved

Populating A Div

Posted on 2013-05-12
4
Medium Priority
?
263 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:Sandeep Kothari
Sandeep Kothari 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

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

595 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