legnoart
11-27-2012, 07:50 PM
hi,
I'm pulling data from a database and want to alter the height of the td class "space", depending on the amount of rows.
here's what i've got so far:
<?php
$connection = mysql_connect("db12345.db.1and1.com","dbo12345","12345");
mysql_select_db("db12345");
if (!$connection) {
die("Error: " . mysql_error());
} else {
}
echo "<table id=\"mytable\">";
$myresult = mysql_query("SELECT * FROM thetable");
while($row = mysql_fetch_object($myresult))
{
echo "<tr>
<td><p class=\"i\">".nl2br($row->field1)."</p></td>
<td><p class=\"i\">".nl2br($row->field2)."</p></td>
<td><p class=\"i\">".nl2br($row->field3)."</p></td>
<td class=\"space\">This cell should have variable height depending on overall height of table</td></tr></table>";
}
mysql_close("db12345","dbo12345","12345");
?>
<button name="set_height" id="set_height">Set height</button>
<script type="text/javascript">
$('#set_height').click(function() {
var theheight = $("#mytable").height();
alert(theheight);
if (theheight < 1000) {
document.getElementsByClassName("space").style.height = 100 + 'px';
}
});
</script>
Everything works fine, but the cells don't resize. There's some reaction when i assign an id to the td and use document.getElementById("space"), meaning that the first row resizes - at least something.
It's probably necessary to loop through the rows, so I tried
var thelist = document.getElementsByClassName("space");
for (var i = 0; i < thelist.length; i++) {
var elements = thelist[i];
elements.height = 100 + 'px';
}
but that doesn't work.
Can anyone spot a simple syntax mistake, or - being a complete js newb - have i approached things 100% wrong?
Thanks for any hints...
I'm pulling data from a database and want to alter the height of the td class "space", depending on the amount of rows.
here's what i've got so far:
<?php
$connection = mysql_connect("db12345.db.1and1.com","dbo12345","12345");
mysql_select_db("db12345");
if (!$connection) {
die("Error: " . mysql_error());
} else {
}
echo "<table id=\"mytable\">";
$myresult = mysql_query("SELECT * FROM thetable");
while($row = mysql_fetch_object($myresult))
{
echo "<tr>
<td><p class=\"i\">".nl2br($row->field1)."</p></td>
<td><p class=\"i\">".nl2br($row->field2)."</p></td>
<td><p class=\"i\">".nl2br($row->field3)."</p></td>
<td class=\"space\">This cell should have variable height depending on overall height of table</td></tr></table>";
}
mysql_close("db12345","dbo12345","12345");
?>
<button name="set_height" id="set_height">Set height</button>
<script type="text/javascript">
$('#set_height').click(function() {
var theheight = $("#mytable").height();
alert(theheight);
if (theheight < 1000) {
document.getElementsByClassName("space").style.height = 100 + 'px';
}
});
</script>
Everything works fine, but the cells don't resize. There's some reaction when i assign an id to the td and use document.getElementById("space"), meaning that the first row resizes - at least something.
It's probably necessary to loop through the rows, so I tried
var thelist = document.getElementsByClassName("space");
for (var i = 0; i < thelist.length; i++) {
var elements = thelist[i];
elements.height = 100 + 'px';
}
but that doesn't work.
Can anyone spot a simple syntax mistake, or - being a complete js newb - have i approached things 100% wrong?
Thanks for any hints...