How to display emaiIds with hyperlink when using php-mysql - php

I am display the contents of mysql table using php.
Based on the user inputs, I will do some querying and the results results will be stored in an array and displayed.
Now I have emilIds in the output, when I display the table in the webpage, I want to display with hyperlinks.
How can I achieve this. Thanks in advance.
if($result2->num_rows > 0){
echo '<table width="100%" bgcolor="#FFFFFF" cellpadding="0" cellspacing="0" border="1" class="db-table" boarder-collapse ="collapse">';
echo '<tr><th>STUDENT_NAME</th><th>CLASS</th><th>ID_NUMBER</th><th>ADDRESS</th><th>CITY</th><th>STATE</th><th>ZIP</th><th>COUNTRY</th><th>PHONE</th><th>INSURANCE</th>
<th>EMAIL</th><th>MAILING_ADDRESS</th><th>JOINED_ON</th></tr>';
while($rowz2 = mysqli_fetch_assoc($result2)){
echo "<tr>";
foreach($rowz2 as $key=>$value){
echo '<td>',$value,'</td>';
}
echo '</tr>';
}
echo "</table><br/>";
}
else{
echo "<h2>No data based on the entered values</h2>";
}

Use an a tag with mail:
echo '<td>' . $value .'</td>';
Assuming that $value content is an email address
Changes based the code you posted in your comments:
Apparently was a couple of nesting problems but I think what you wanted to say was...
echo "<table>";
while($rowz2 = mysqli_fetch_assoc($result2)){
echo "<tr>";
foreach($rowz2 as $key=>$value){
if($key=='EMAIL') {
echo '<td>' . $value . '</td>';
} else {
echo '<td>' . $value . '</td>';
}
}
echo '</tr>';
}
echo "</table><br/>";
I can't say for sure if that would work as you expect but if a value is retrieved from DB with a field name "EMAIL" then it will be echoed inside a mailto: link.
So whether the field name is EMAIL or you used SELECT this_field, that_field, the_email_field as EMAIL FROM your_table it would bring a working mailto: link in your table.

I cannot comment yet so i will post it here. Check if your database column is 'email' or 'EMAIL' because
if ($key == 'EMAIL')
is case sensitive and as such it will never pass the condition if column is 'email'. If for some reason you need a case insensitive string comparison use:
if (strcasecmp($key, 'EMAIL') === 0)

Related

PHP table alignment is not correct inside foreach Loop

I want to display Array data in a table with 3 columns in a row using foreach loop with a condition.
Coding
$value[]='a';
$value[]='b';
$value[]='c';
$value[]='d';
$value[]='e';
echo '<table width=30% border=1>';
echo '<tr>';
$counter=1;
foreach($value as $key){
if($counter>=3){ // if there is more than 3 elements, go to next Row
if($counter%3==0){ // when the Array hit 3th,6th,9th,12th.... element
echo '</tr><tr><td>';
echo $key;
echo '</td>';
}else{
echo '<td>';
echo $key;
echo '</td>';
}
}else{
echo '<td>';
echo $key;
echo '</td>';
}
$counter++;
}
echo '</tr>';
echo '</table>';
I double check the coding and didn't manage to find the error.... my output is the bottom of the image. However, the correct one should be top of the image. Please take a look at the photo
Anyone know what's wrong with my coding?
Change modulas condition with
if($counter % 3 == 1)
and you will get what you desire
You have "if counter >= 3" so you are hitting that condition on your 3rd element, not AFTER your third element.
Change the initialization of your counter to $counter=0;

MySQL select where column LIKE and is not empty

I have the following query and I'm trying to select only the rows where I have picture.
In table kat_gl_picture I have 3 categories, but I don't have picture in all 3 categories yet!
All work just fine, but I have printed name of third category, where I don't have picture.
I tried WHERE link LIKE '%$first_var%'AND NOT (link <=> NULL)
....IS NOT NULL - but nothing yet worked.
Tabele1 and 2 and web problem solved
<?
include("connection.php");
$kategorije = mysql_query("SELECT * FROM kat_gl_picture ORDER BY rbr");
while ($red=mysql_fetch_array($kategorije))
{
$first_var = $red['kat'];
$result = mysql_query("SELECT id, naziv, ime, tekst, username, link, file_name, datum FROM Tab_Pic_Pic
WHERE link LIKE '%$first_var%'
ORDER BY id");
echo '<table>';
echo '<tbody>';
echo $first_var;
echo '<tr>';
echo '<TD valign="top">';
while ($row=mysql_fetch_array($result))
{
list($x, $y) = getimagesize("admin /upload/".$row['file_name']);
if ($x>$y) {
$y=($y/$x)*150;
$x=150;
}
else
{
$x=($x/$y)*115;
$y=115;
}
$ID_broj = $row["id"];
$tekst_broj = $row["tekst"];
?>
<? echo '<img src="admin /upload/'.$row['file_name'].'" height="'.$y.'" width="'.$x.'"/>';?>
<?
}
echo '</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
}
The problem is that I cannot determine if there is a foreign key in your Tab_Pic_Pic table. You can however join on a LIKE.
Try this:
SELECT * FROM Tab_Pic_Pic JOIN kat_gl_picture ON Tab_Pic_Pic.link LIKE CONCAT('%', kat_gl_picture.kat, '%') ORDER BY kat_gl_picture.kat, Tab_Pic_Pic, rbr
It should give you a list of all pictures including the kat_gl_picture.kat field which you can just use a local variable to detect change.
Would be easier to provide a more accurate example if I had the full table info. Standard method is to have a foreign key in Tab_Pic_Pic that references the corresponding primary key from the kat_gl_picture table.
Alternatively, if you simply want to omit the blank category it can be done in your PHP code like this:
if(mysqli_num_rows($result)>0){
echo '<table>';
echo '<tbody>';
echo $first_var;
echo '<tr>';
echo '<TD valign="top">';
while ($row=mysql_fetch_array($result))
{
list($x, $y) = getimagesize("admin /upload/".$row['file_name']);
if ($x>$y) {
$y=($y/$x)*150;
$x=150;
}
else
{
$x=($x/$y)*115;
$y=115;
}
$ID_broj = $row["id"];
$tekst_broj = $row["tekst"];
?>
<? echo '<img src="admin /upload/'.$row['file_name'].'" height="'.$y.'" width="'.$x.'"/>';?>
<?
}
echo '</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
}
Wrapping the output of your HTML in a simple condition like this should illustrate that the script itself is inefficient. Trust me when I say it can all be accomplished with one query. Do not get frustrated. You'll figure it out. Programming is hard.

Creating a HTML table with MYSQL fetching data doesn't work

I'm attempting to create a dynamic table with MYSQL's function: while(mysql_fetch_assoc).. However, when it fetches more than one result, it doesn't create the table anymore (or fill in the tags. Excuse me for explaining this incorrectly)
This is my code. Ignore the Dutch words :)
$sql2 = mysql_query("SELECT * FROM kostendb WHERE ProjectID = '$_GET[id]'") or die (mysql_error());
echo '
<table border="1" style="width:60%">
<tr>
<th>Kostencode</th>
<th>Datum</th>
<th>Bedrag</th>
</tr>';
while($res = mysql_fetch_assoc($sql2))
{
echo '<tr>';
echo '<td>' .$res['KostenID']. '</td>';
echo '<td>' .$res['Datum']. '</td>';
echo '<td>' .$res['Bedrag']. '</td>';
echo '</tr>';
}
echo '</table>';
When it finds more than one result, the while-loop doesn't do anything. When it finds just one result, it works fine.
What is causing this, and how can I fix this?
I've checked out an example script, but it's exactly using my method.
Thanks
You might have mixed mysql with mysqli.
Choose one, don't mix and this might fix your problem.
make a variable like $project = $_GET['ID'];
then put in sql statement as ....WHERE Project_ID = $project");
Try this

How do I get a php table to print a range of input values but, ignore the ones that are not filled in?

I have made a form for a company website that the user will populate with personal info. I want to use php to handle these values and then render them as a table that can be printed. I have done this before but only for forms where everything is filled in. I want to make this one to where if the user decides to leave a field blank then you will not see it in the php rendered page. I will post a small example below for reference to help with my issue, as of now I have no idea where to start.
Example form...
<form action="post">
<p><input type="text" id="name" name="name"></p>
<p><input type="text" id="phone" name="phone"></p>
<p><input type="text" id="job" name="job"></p>
</form>
In this example form I am gathering name, phone number, and occupation. At the bottom I would have a button to submit the info. A php doc will then handle the values and render them in a table set up like this...(assume I $_POST the variables above this table.)
'<table>
<tr>
<td>Name:' .$name. '</td>
<td>Phone:' .$phone. '</td>
<td>Job:' .$job. '</td>
</tr>
</table>'
This works fine however if the customer decides NOT to give there phone number it looks like this...
Name: Alex
Phone:
Job: Fireman
What I need to do is make it to where if they DO fill out the phone number it will show it however if they do not I need it to look like this...
Name: Alex
Job: Fireman
My actual form is very long however I thought this example would be enough for someone to help me get started, thanks in advance for your feedback.
Loop through the $_POST array and print out a key-value when it has value.
foreach($_POST as $key=>$value)
{
if($value != '')
{
echo ucwords($key) . ': ' . $value;
}
}
OR
With table formatting
// Just set the table up as necessary
echo '<table><tr>';
foreach($_POST as $key=>$value)
{
if($value != '')
{
echo '<td>' . ucwords($key) . ': </td><td>' . $value . '</td>;
}
}
echo '</tr></table>';
The answer is using the empty function of PHP.
Try this in your processing PHP file:
echo "<table><tr>";
if(!empty($name)) echo "<td>Name: $name</td>";
if(!empty($phone)) echo "<td>Phone: $phone</td>";
if(!empty($job)) echo "<td>Job: $job</td>";
echo "</tr></table>";
Something like this, based on your string.
$table = '<table>
<tr>';
if (!empty($name)) {
$table .= '<td>Name:' .$name. '</td>';
}
if (!empty($phone)) {
$table .= '<td>Phone:' .$phone. '</td>';
}
if (!empty($job)) {
$table .= '<td>Job:' .$job. '</td>';
}
$table .= '</tr>
</table>';
echo $table;

PHP and SQLite 3, multiple returned row

I have a problem reading out multiple rows that get returned from an SQLite 3 database.
I tested my querys before and i know what the result should be. But i cant seem to figure out how to read these different lines/rows that get returned from this query on a PHP based webpage.
I searched the web, including stackoverflew but i can't seem to find an anwser to this question :/
Any help would be great!
Regards
P.S. this is how i fetch my the multiple rows on my php page:
$ID[$i] = $dbase->querySingle("SELECT rowid FROM rules WHERE parameter".$i." = '$Model'");
I have this in PHP:
$query = $db->query("SELECT * FROM files WHERE OBJECT = '".$id."'") ;
// begin table
echo '<table bgcolor="#FFFFFF">'."\n";
// give titles
echo '<tr>';
echo '<td bgcolor="#669999"><b><u>File</u></b></td>';
echo '<td bgcolor="#669999"><b><u>Night</u></b></td>';
echo '<td bgcolor="#669999"><b><u>Name</u></b></td>';
echo '<td bgcolor="#669999"><b><u>comment</u></b></td>';
echo '</tr>'."\n";
//
while($row = $query->fetchArray()) {
echo '<tr>';
echo '<td bgcolor="#CCCCCC">'.$row["uniquenumber"].'</td>';
echo '<td bgcolor="#CCCCCC">'.$row["NIGHT"].'</td>';
echo '<td bgcolor="#CCCCCC">'.$row["NAME"].'</td>';
echo '<td bgcolor="#CCCCCC">'.$row["COMMENTS"].'</td>';
echo '</tr>'."\n";
}
It works here.
Hope it can help you !

Categories