I am trying to output a table using php variables like this:
echo "<tr>";
echo "<td>".$var1."</td>";
echo "<td>".$var2."</td>";
echo "</tr>";
It works fine if both variables exist.. but if $var1 is null, the $var2 value is outputted to the firs column in the HTML table.
Is there a way to properly output the columns by naming them?
P.S: I can't do "if $var == NULL ...." for some coding reasons. I want to control the HTML output if possible.
While I agree with #leeb above, do you want something like this:
echo "<td>" . isset($var1)?$var1:"" . "</td>";
echo "<tr>";
echo "<td>".$var1." </td>";
echo "<td>".$var2." </td>";
echo "</tr>";
Related
i need some little help ,,,
I have a data like this.., (I use php mysql)
Initially there was no problem with this, but as time goes by, my data is getting bigger and bigger.
finally its made my program slow ..
I think maybe because I used "while" inside of "while". make SQL is called multiple times.
is there a solution to make it faster ??
I have hundreds of data in tb1 and thousands in tb2 T.T
You should not be using loops to iterate over tables. SQL is an inherently set based declarative language. So, you should just join the tables, order the result set, and then use a single loop with presentation logic. Use this query:
SELECT tb1.id_a, tb2.id_b, tb2.data
FROM tb1
INNER JOIN tb2 ON tb2.id_a = tb1.id_a
Then, use this PHP script:
echo "<table>";
echo "<tr><th>No.</th><th>id_b</th><th>data</th></tr>";
$a = null;
while ($row = mysql_fetch_array($result)) {
if ($a == null || $row['id_a'] != $a) {
echo "<tr>";
echo "<td>" . $row['id_a'] . "</td>";
echo "<td colspan=2>data" . $row['id_a'];
echo "</tr>";
$a = $row['id_a'];
}
echo "<tr>";
echo "<td></td>";
echo "<td>" . $row['id_b'] . "</td>";
echo "<td>" . $row['data'] . "</td>";
echo "</tr>";
}
echo "</table>";
I have two MySQL tables with number of columns. The table structure is given below,
1.pictures
postedON
caption
imageName
thumbName
imageLocation
thumbLocation
2.Videos
postedOn
category
Link
I am using the folowing PHP function to fetch data from DB using a select command.
function select($table){
if($this->db_connection){
$query = 'SELECT * FROM '. $table;
$result = mysqli_query($this->db_connection,$query) or die($this->db_connection->error);
//print_r($result);
//echo "Affected rows: " . mysqli_affected_rows($this->db_connection);
//var_dump($result);
echo "<table>";
echo "<tr>";
echo "<th>Date Posted</th>";
echo "<th>Category</th>";
echo "<th>Link</th>";
echo "</tr>";
while($row = $result->fetch_assoc()){
echo "<tr>";
echo "<td>" . $row['postedOn'] . "</td>";
echo "<td>".$row['category']. "</td>";
echo "<td>" . $row['link'] . "</td>";
echo "</tr>";
}
echo "</table>";
}else{
echo "db_connection is = " . $this->db_connection;
}
}
}
The problem with this function as you can see, it can only serve only one table and not dynamic. Can someone please explain the way to dynamically fetch data from different table with different number of columns using only one PHP function? Thanks
Try using mysqli_fetch_fields()
<?php
function select($table){
if($this->db_connection){
$query = 'SELECT * FROM '. $table;
$result = mysqli_query($this->db_connection,$query) or die($this->db_connection->error);
$fieldinfo = mysqli_fetch_fields($result);
echo "<table>";
echo "<tr>";
foreach ($fieldinfo as $val)
{
echo "<th>".$val->name."</th>";
}
echo "</tr>";
while($row = $result->fetch_assoc()){
echo "<tr>";
foreach ($fieldinfo as $val)
{
echo "<td>" . $row[$val->orgname] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}else{
echo "db_connection is = " . $this->db_connection;
}
}
It could be hard to explain given all the wrong premises you approach is based on, but I'll try.
First of all, you have to understand that a query like SELECT * FROM table has a very little use, next to none. Most of time you are always have some WHERE or at least LIMIT clause. So, such a function will have no use at all.
Next, you have to learn by heart that database interaction should never be intermixed with any output stuff like HTML.
Given these two premises above, your function should accept a fill qualified query as a parameter and return an array with data as a result.
For which array, in turn you can write a helper function to display its contents in the form of HTML table.
But again, such a generalized output function will be of little use as well, because, as you can see from your own example, different fields will need different formatting. So it's better to write output each time by hand.
I'm coming here after some head banging. Still learning php/mysql and coming across an issue which is bothering me for a while.
Currently I print a table with all the data without any condition. The values below the lower values and above are printed in this table. The snippet of my code is below:
while ($result = mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>" .$result['id']. "</td>";
if ($result['time'] > $limit[lower]){
echo "<td bgcolor=#ff0000>" $result['time']. "</td>";
} else {
echo "<td>" .$result['time']. "</td>";
}
echo "<td>" .$result['name']. "</td>";
echo "</tr>";
}
I want to create a href link so that by dafault only values above the lower are printed and on clicking the link, say detailed_view will give the table as it does above i.e. all value including below and above the lower value. Shall I create two methods and use them in href separately. In that case there will be two while loops which I want to avoid. Any pointer on this?
The name of the script is display.php, so before the loop I tried using:
echo "<a href='display_jobid.php?view=$num'>Show All</a>";
It does create a hyperlink Show All but how to get the functionality working, am not sure.
Thanks
my id is not going through the url. my code is as follows
<?php
include 'library/connect.php';
$result = mysql_query("SELECT * FROM meetings ");
echo "<table border='1'><tr><th>Title</th><th>Chairman</th><th>Secretary</th><th>Terms of Reference</th><th>Named membership</th><th>Occurences</th><th>Book Room</th></tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title']. "</td>";
echo "<td>" . $row['chairman']. "</td>";
echo "<td>" . $row['secretary']. "</td>";
echo "<td>" . $row['termsOfReference']. "</td>";
echo "<td>" . $row['named_membership']. "</td>";
echo "<td>" . $row['occurences']. "</td>";
?>
<td><font color="#CC3300">Book: room/date/time</font></td>
<?php
}
echo "</tr>";
echo "</table>";
include 'library/closedb.php';
?>
have you got any idea of what the problem can be?
where is $meeting_id set? seems to me like it should be $row['meeting_id'].
Where $meeting_id is defined?
It seems like you did a select but forgot to retrieve the meeting id.
Try to change the link to:
<a href ="secretary_booksRoom.php?meeting_id=<?php echo $row['meeting_id']; ?>">
In case you have column named meeting_id in your table of course.
<tr> tag need to be closed in the while loop
You need to define the $meeting_id variable before adding it to the URL, otherwise your link will simply be "secretary_booksRoom.php?meeting_id=". I am going to assume that your meetings table has an id column as a primary key. In your while loop, try declaring "$meetind_id = $row['your meeting id column']". This should get the meeting id and pass it to the URL.
Hope this helps.
Not sure why this will not work? Does the loop not like my variable? If I hard code this workings...
while($row = mysql_fetch_array($resultno))
{
echo "<tr>";
echo "<td>" . $row['$radio'] . "</td>";
echo "<td>" . $row['count'] . "</td>";
echo "</tr>";
}
'$radio' is a string, not a variable. Remove the apostrophes and make it into $radio:
$row[$radio]
This will make it possible to choose a column from the MySQL resultset by setting $radio to the chosen value.
Your row is not likely to have a $radio key; maybe you meant $row['radio']. Or $row[$radio].
Your array referencing is wrong:
$row['$radio']
should be written as follows, when referencing an array element using another variable
$row[$radio]
or as follows when accessing an element name
$row['radio']
$row['$radio'] seems to be a problem. Is $radio a variable with a value you would like to use as index for the $row then write $row[$radio] otherwise if the table column is named "radio" only write $row['radio'].