Jump to content


Photo

How do I "echo" within "echo"?


  • Please log in to reply
5 replies to this topic

#1 FourEyes

FourEyes
  • Members
  • Pip
  • Newbie
  • 4 posts
  • LocationMichigan

Posted 14 June 2006 - 08:47 PM

Here is the code:

<?php
session_start();
header("Cache-control: private"); 
$person_id = $_SESSION['page'];
$customer_id = $_SESSION['stamp'];
$c = $_SESSION['c'];
$m1 = $_SESSION['m1'];
$m2 = $_SESSION['m2'];
$plan_id = $_POST['Plans'];
$medical = array(array());
include 'header.html';

# Connect to MySQL
$connection = @mysql_connect("localhost", "??", "??") or die("Could not connect to MySQL.");

# Select the specified database.
$result = @mysql_select_db("sfb", $connection) or die("Could not select the requested database.");
?>

<table width=175 border=0 align="left" cellpadding=0 cellspacing=0>
<tr align="left" valign="top" nowrap>
  <td width=175 valign="top" nowrap><img src="../images/NAV-pic.jpg" border=0></td>
</tr><tr align="left" valign="top" nowrap>
  <td align="left" nowrap><a href="index.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image6','','NAV-home-on.gif',0)"><img src="../images/NAV-home.gif" alt="home" name="Image6" border=0></a></td>
</tr><tr align="left" valign="top" nowrap>
  <td align="left" nowrap><a href="products.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image7','','NAV-products-on.gif',0)"><img src="../images/NAV-products.gif" alt="Products & Services" name="Image7" border=0></a></td>
</tr><tr align="left" valign="top" nowrap>
  <td align="left" nowrap><a href="csa.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image19','','NAV-service-on.gif',0)"><img src="../images/NAV-service-on.gif" alt="Customer Service Area" name="Image19" border=0></a></td>
</tr><tr align="left" valign="top" nowrap>
  <td align="left" nowrap><a href="quotes.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image9','','NAV-quotes-on.gif',0)"><img src="../images/NAV-quotes.gif" alt="Online Quotes" name="Image9" border=0></a></td>
</tr><tr align="left" valign="top" nowrap>
  <td align="left" nowrap><a href="free.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image17','','NAV-info-on.gif',0)"><img src="../images/NAV-info.gif" alt="Free Information" name="Image17" border=0></a></td>
</tr><tr align="left" valign="top" nowrap>
  <td align="left" nowrap><a href="about.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image18','','NAV-about-on.gif',0)"><img src="../images/NAV-about.gif" alt="About Us" name="Image18" width=175 height=24 border=0></a></td>
</tr>    
</table>

<?php
$self = $_SERVER['PHP_SELF'];
echo ("<form action=\"$self\" method=\"post\">");
?>

<h2><?php echo $c;?><br>Add/Edit/Delete Major Medical Plan</h2>
    <select name="Plans" size=5>
<?php
$query = "select * from medical order by plan_name";
$result = mysql_query($query,$connection) or die ("Could not find any plans.");

while ($row = mysql_fetch_assoc($result)) {
  $id = $row['plan_id'];
  if ($id == $plan_id) {
    echo ("  <option selected value=");
  } else {
    echo ("  <option value=");
  }
  echo $id . ">" . $row['plan_name'] . " ~ " . $row['carrier'];
  if ($row['network'] == "Y") {
    echo " ~ Network</option>\n";
  } else {
    echo " ~ Non-network</option>\n";
  }

  $j = 0;
  foreach ($row as $info) {
    $medical[$id][$j] = $info;
    $j++;
  }
}
?>
</select><br>
Select a plan, then click, <input type="submit" name="View" value="View Plan Details"><br><br>
<table width=595 border=0 cellpadding=0 cellspacing=0>
<?php
if ($plan_id > 0) {
  if ($medical[$plan_id][3] == "Y") {
    echo "<tr><td>" . $medical[$plan_id][$k] . "</td></tr>";
  } else {
    for ($k=4;$k<$j;$k=$k+7) {
      echo "<tr><td>" . $medical[$plan_id][$k] . "</td><td><input name=\"field" . $k . " size=45 value=\"<?php echo isset($_POST['View'])?$row['customer_address1']:\'\';?>\"></td></tr>";
    }
  }
} else {
  for ($k=4;$k<$j;$k=$k+7) {
    echo "<tr><td>" . $medical[0][$k] . "</td></tr>";
  }
}
?>
</table>
<!--#include virtual="footer.html" -->

The problem is in the line:

      echo "<tr><td>" . $medical[$plan_id][$k] . "</td><td><input name=\"field" . $k . " size=45 value=\"<?php echo isset($_POST[\'View\'])?$row[\'customer_address1\']:\'\';?>\"></td></tr>";
The last pair of \' cause no problem, but all of the other \' cause the parser to halt (I think that's the right way to say it - nothing appears on the Web page).

I know that I can put $_POST['View'] and $row['customer_address1'] into variables outside of this echo statement and be done with the problem, but I'd like to learn how to do this correctly, rather than just make it work. What do I do to make the code work correctly?

#2 tidalik

tidalik
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 14 June 2006 - 08:52 PM

[!--quoteo(post=383984:date=Jun 14 2006, 03:47 PM:name=FourEyes)--][div class=\'quotetop\']QUOTE(FourEyes @ Jun 14 2006, 03:47 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Here is the code:

echo "<tr><td>" . $medical[$plan_id][$k] . "</td><td><input name=\"field" . $k . " size=45 value=\"<?php echo isset($_POST[\'View\'])?$row[\'customer_address1\']:\'\';?>\"></td></tr>";

The last pair of \' cause no problem, but all of the other \' cause the parser to halt (I think that's the right way to say it - nothing appears on the Web page).

I know that I can put $_POST['View'] and $row['customer_address1'] into variables outside of this echo statement and be done with the problem, but I'd like to learn how to do this correctly, rather than just make it work. What do I do to make the
[/quote]


Why are you calling
<?php
again? It's already open...

Have you just tried just removing the <?php, ?> & second echo?

#3 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 14 June 2006 - 09:32 PM

Have a look at heredoc (see signature).
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#4 JP128

JP128
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationWisconsin

Posted 15 June 2006 - 06:58 AM

echo "<tr><td>" . $medical[$plan_id][$k] . "</td><td><input name=\"field" . $k . " size=45 value=\"<?php echo isset($_POST[\'View\'])?$row[\'customer_address1\']:\'\';?>\"></td></tr>";

Lets break this down... I have a question. Are you trying to add something to the name? name=field$k? Also, if you want to echo something, why not just turn it into a variable?

$new_variable = "isset($_POST[\'View\'])?$row[\'customer_address1\']:\'\';?>\"></td></tr>";

I also agree with tidalik... How come you are re-opening the <?php tag? That doesn't make sense to me.

idk, maybe I am wrong, but that is what I may do... I am new to php, trying to learn a lot, but when it comes to these things, I may be wrong. So, I hope I helped in any way?
-----------------------------------------------------------------------
John Ensign - Age 16 - PHP Learner

My email is Johnny.Ensign@gmail.com

#5 FourEyes

FourEyes
  • Members
  • Pip
  • Newbie
  • 4 posts
  • LocationMichigan

Posted 16 June 2006 - 11:47 AM

[!--quoteo(post=383985:date=Jun 14 2006, 04:52 PM:name=tidalik)--][div class=\'quotetop\']QUOTE(tidalik @ Jun 14 2006, 04:52 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Why are you calling
<?php
again? It's already open...

Have you just tried just removing the <?php, ?> & second echo?
[/quote]


I did. No change, I still have a blank page.

[!--quoteo(post=383992:date=Jun 14 2006, 05:32 PM:name=effigy)--][div class=\'quotetop\']QUOTE(effigy @ Jun 14 2006, 05:32 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Have a look at heredoc (see signature).
[/quote]

I read it. I don't see how this applies to my situation.

[!--quoteo(post=384088:date=Jun 15 2006, 02:58 AM:name=JP128)--][div class=\'quotetop\']QUOTE(JP128 @ Jun 15 2006, 02:58 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
echo "<tr><td>" . $medical[$plan_id][$k] . "</td><td><input name=\"field" . $k . " size=45 value=\"<?php echo isset($_POST[\'View\'])?$row[\'customer_address1\']:\'\';?>\"></td></tr>";

Lets break this down... I have a question. Are you trying to add something to the name? name=field$k? Also, if you want to echo something, why not just turn it into a variable?

$new_variable = "isset($_POST[\'View\'])?$row[\'customer_address1\']:\'\';?>\"></td></tr>";

I also agree with tidalik... How come you are re-opening the <?php tag? That doesn't make sense to me.

idk, maybe I am wrong, but that is what I may do... I am new to php, trying to learn a lot, but when it comes to these things, I may be wrong. So, I hope I helped in any way?
[/quote]


Okay, re-opening PHP was ignorant on my part. Also, to solve the problem, I did as you suggest, which is put the variables into other variables that won't require quotes. It's not elegant, but it works. I just thought there was a less brute-force way.

      $aa = $_POST['View'];
      $bb = $row['customer_address1'];
      echo "<tr><td>" . $medical[$plan_id][$k] . "</td><td><input name=\"field" . $k . " size=45 value=\"isset($aa)?$bb:\'\';\"></td></tr>";


Thank you all for your suggestons.

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 16 June 2006 - 11:53 AM

Try changing your problem code:
<?php
echo "<tr><td>" . $medical[$plan_id][$k] . "</td><td><input name=\"field" . $k . " size=45 value=\"<?php echo isset($_POST[\'View\'])?$row[\'customer_address1\']:\'\';?>\"></td></tr>";
?>
to
<?php
echo '<tr><td>' . $medical[$plan_id][$k] . '</td><td><input name="field' . $k . '" size=45 value="';
echo isset($_POST['View'])?$row['customer_address1']:'';
echo '"></td></tr>';
?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users