HighDots Forums  

PHP / MySQL - Printing a Variable within a Table Cell

Macromedia Dreamweaver Macromedia Dreamweaver Discussions (macromedia.dreamweaver)


Discuss PHP / MySQL - Printing a Variable within a Table Cell in the Macromedia Dreamweaver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
ArizonaJohn
 
Posts: n/a

Default PHP / MySQL - Printing a Variable within a Table Cell - 03-17-2009 , 01:00 AM






Hello,

The attached code works great. In an HTML table, it prints the "votes_up"
value from a MySQL database for each "$row2" generated by a "while" loop.

However, in an HTML table cell, I would like to print "votes_up" minus another
value called "votes_down" I imagine that the first step would be to create a
variable equal to "votes_up" minus "votes_down." But I don't know how to print
this new variable for each "$row2" in the MySQL database. How do I do this?

Thanks in advance,

John

print "<td>" .$row2['votes_up']. "</td>";


Reply With Quote
  #2  
Old   
Michael Fesser
 
Posts: n/a

Default Re: PHP / MySQL - Printing a Variable within a Table Cell - 03-17-2009 , 04:56 AM






..oO(ArizonaJohn)

Quote:
The attached code works great. In an HTML table, it prints the "votes_up"
value from a MySQL database for each "$row2" generated by a "while" loop.

However, in an HTML table cell, I would like to print "votes_up" minus another
value called "votes_down" I imagine that the first step would be to create a
variable equal to "votes_up" minus "votes_down." But I don't know how to print
this new variable for each "$row2" in the MySQL database. How do I do this?

Thanks in advance,

John

print "<td>" .$row2['votes_up']. "</td>";
$votes_diff = $row2['votes_up'] - $row2['votes_down'];
print "<td>$votes_diff</td>";

or

print "<td>".($row2['votes_up'] - $row2['votes_down'])."</td>";

HTH
Micha


Reply With Quote
  #3  
Old   
Murray *ACE*
 
Posts: n/a

Default Re: PHP / MySQL - Printing a Variable within a Table Cell - 03-17-2009 , 08:15 AM



The problem with that is that DW's design view won't understand it. I'd
rather see you do it this way -

<? $votes_diff = $row2['votes_up'] - $row2['votes_down']; ?>
<td><?php echo $votes_diff?$votes_diff:'&nbsp;'; ?></td>

which will retain the validity of the HTML on your page.

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"Michael Fesser" <netizen (AT) gmx (DOT) de> wrote

Quote:
.oO(ArizonaJohn)

The attached code works great. In an HTML table, it prints the
"votes_up"
value from a MySQL database for each "$row2" generated by a "while" loop.

However, in an HTML table cell, I would like to print "votes_up" minus
another
value called "votes_down" I imagine that the first step would be to
create a
variable equal to "votes_up" minus "votes_down." But I don't know how to
print
this new variable for each "$row2" in the MySQL database. How do I do
this?

Thanks in advance,

John

print "<td>" .$row2['votes_up']. "</td>";

$votes_diff = $row2['votes_up'] - $row2['votes_down'];
print "<td>$votes_diff</td>";

or

print "<td>".($row2['votes_up'] - $row2['votes_down'])."</td>";

HTH
Micha


Reply With Quote
  #4  
Old   
Michael Fesser
 
Posts: n/a

Default Re: PHP / MySQL - Printing a Variable within a Table Cell - 03-17-2009 , 01:18 PM



..oO(Murray *ACE*)

Quote:
The problem with that is that DW's design view won't understand it. I'd
rather see you do it this way -

? $votes_diff = $row2['votes_up'] - $row2['votes_down']; ?
td><?php echo $votes_diff?$votes_diff:'&nbsp;'; ?></td

which will retain the validity of the HTML on your page.
Why the ternary operator? What if the difference becomes 0?

Micha


Reply With Quote
  #5  
Old   
Cosmo Kramer
 
Posts: n/a

Default Re: PHP / MySQL - Printing a Variable within a Table Cell - 03-17-2009 , 01:57 PM



Alternatively:

Here's your query:

select (`votes_up` - `votes_down`) as votes_total from table;

Then your HTML/PHP

echo "<td>" .$row2['votes_total']. "</td>";



"ArizonaJohn" <webforumsuser (AT) macromedia (DOT) com> wrote

Quote:
Hello,

The attached code works great. In an HTML table, it prints the "votes_up"
value from a MySQL database for each "$row2" generated by a "while" loop.

However, in an HTML table cell, I would like to print "votes_up" minus
another
value called "votes_down" I imagine that the first step would be to
create a
variable equal to "votes_up" minus "votes_down." But I don't know how to
print
this new variable for each "$row2" in the MySQL database. How do I do
this?

Thanks in advance,

John

print "<td>" .$row2['votes_up']. "</td>";




Reply With Quote
  #6  
Old   
Michael Fesser
 
Posts: n/a

Default Re: PHP / MySQL - Printing a Variable within a Table Cell - 03-17-2009 , 02:40 PM



..oO(Cosmo Kramer)

Quote:
Alternatively:

Here's your query:

select (`votes_up` - `votes_down`) as votes_total from table;

Then your HTML/PHP

echo "<td>" .$row2['votes_total']. "</td>";
CHEATER!!!1

;-)

OK, of course that would be another (and in fact the preferred) way.
But you should remove the backticks - they're just damn ugly and not
necessary in this case.

Micha


Reply With Quote
  #7  
Old   
ArizonaJohn
 
Posts: n/a

Default Re: PHP / MySQL - Printing a Variable within a TableCell During a While Loop - 03-18-2009 , 12:04 AM



Wow, it was cool to see so many responses. I really appreciate all of your
answers. There is obviously a lot of knowledge floating around in this forum.

Michael Fesser, thanks, I ended up using your response, and it works.

Murray *ACE*, thanks for the response. I code in Dreamweaver, but I then just
upload to my host. I don't really bother to open Dreamweaver's design view.

Cosmo Kramer, thanks for your response. Your response looked like an easy
solution, but I tried it and the HTML just printed out the code. Was it
supposed to be within PHP or something?


-John


Reply With Quote
  #8  
Old   
Cosmo Kramer
 
Posts: n/a

Default Re: PHP / MySQL - Printing a Variable within a Table Cell During a While Loop - 03-18-2009 , 09:05 AM




"ArizonaJohn" <webforumsuser (AT) macromedia (DOT) com> wrote


Quote:
Cosmo Kramer, thanks for your response. Your response looked like an easy
solution, but I tried it and the HTML just printed out the code. Was it
supposed to be within PHP or something?
Yes, of course. That was a given...


<?php

echo "<td>" .$row2['votes_total']. "</td>";

?>




Reply With Quote
  #9  
Old   
Murray *ACE*
 
Posts: n/a

Default Re: PHP / MySQL - Printing a Variable within a Table Cell - 03-23-2009 , 07:30 AM



The reason for the ternary operator was to insert a non-breaking space in
the event that the value were null. This will prevent border/background
color anomalies in some browsers when there is nothing in the table cell.

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"Michael Fesser" <netizen (AT) gmx (DOT) de> wrote

Quote:
.oO(Cosmo Kramer)

Alternatively:

Here's your query:

select (`votes_up` - `votes_down`) as votes_total from table;

Then your HTML/PHP

echo "<td>" .$row2['votes_total']. "</td>";

CHEATER!!!1

;-)

OK, of course that would be another (and in fact the preferred) way.
But you should remove the backticks - they're just damn ugly and not
necessary in this case.

Micha


Reply With Quote
  #10  
Old   
Michael Fesser
 
Posts: n/a

Default Re: PHP / MySQL - Printing a Variable within a Table Cell - 03-24-2009 , 03:37 PM



..oO(Murray *ACE*)

Quote:
The reason for the ternary operator was to insert a non-breaking space in
the event that the value were null. This will prevent border/background
color anomalies in some browsers when there is nothing in the table cell.
Understood, but IMHO this can't happen here. The result will always be a
number, so the cell will always have content.

Micha


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.