The php-code below finds and displays an external url as a link from a database. Sometimes there is a phonenumber instead of an url in that row. How can I manage to echo out the phonenumber without making it to a link. I guess it will work with if and else but I'm not finding out how to display it the right way. My code below:
<?php
$ticketurl = $row ['show_external_url'];
$query = mysqli_query( $connection,"SELECT *, DATE_FORMAT(`show_date`, '%W (%d/%m/%y)') as dateFormatted
FROM nhto_gigpress_shows
WHERE `show_date` >= CURDATE()
AND WEEKDAY(nhto_gigpress_shows.show_date) >= 5
ORDER By show_date" );
$row = mysqli_fetch_array( $query ); {
echo "<a href='".$ticketurl."'>BOOK</a>";
}
?>
Try to check whether the column has any data or not and use condition
inside the while loop:
if(empty($row['url'])){
echo $row['phone'];
}
else{
echo 'BOOK';
}
Related
First I want to explain my application:
I have created an application for my QA work. The application generates a document gets saved as a pdf and added to the server. I have a dynamic table in it that gets save to the database using the implode function separating it as with a comma in the same row as the test case on the database.
It all works fine, but when I want to view the test case I am having trouble to figuring out how to get it to display. I have read plenty of scenarios to use the explode but no luck...
<?php include 'app_database/database.php'; ?>
<?php
if(isset($_POST)){
$step = $_REQUEST['step'];
$url = $_REQUEST['url'];
$pass_fail = $_REQUEST['pass_fail'];
$comment = $_REQUEST['comment'];
$sql1 ="UPDATE qa_testing_application SET step='".implode(',',$step)."',url='" . implode(',',$url) . "',pass_fail='" . implode(',',$pass_fail) . "',comment='" . implode(',',$comment) . "' WHERE test_case_name='$test_case_name'";
$result= mysqli_query($database, $sql1) or die(mysqli_error($database));
}
?>
I am inserting it this way. And i would like to retrieve it from the DB.
I would love to display it as follows:
Please see the link http://i.stack.imgur.com/6aglk.jpg
At the moment i am trying to test and figure out how to display it:
Not sure how to implement a for or foreach function in here as well if thats needed.
$countsteps = 0;
$counturls = 0;
$countpass_fails = 0;
$countcomments = 0;
$test_case_number = '21';
$select_tbl=mysqli_query($database,"select * from qa_testing_application WHERE test_case_number='$test_case_number'");
$result = mysqli_query($database, $sql1) or die(mysqli_error($database));
while($fetch=mysqli_fetch_object($result))
{
$step=$fetch->step;
$url=$fetch->url;
$pass_fail=$fetch->pass_fail;
$comment=$fetch->comment;
$steps=explode(",",$step);
$urls=explode(",",$url);
$pass_fails=explode(",",$pass_fail);
$comments=explode(",",$comment);
echo '<td>'.$steps[$countsteps++].'</td>';
echo '<td>'.$urls[$counturls++]."</td>";
echo '<td>'.$pass_fails[$countpass_fails++]."</td>";
echo '<td>'.$comments[$countcomments++]."</td>";
}
So how would I get this to display in a table?
edit:
Oh and this is the error that I get:
Undefined Offset
This error simply says there is no such key exists into given array or you're trying to fetch a value from non-array variable.
To show data into tabular format, you don't need to explode data coming from db. They are already concatenated.
So to show data from db, modify your code as show below:
$test_case_number = '21';
$select_tbl=mysqli_query($database,"select * from qa_testing_application WHERE test_case_number='$test_case_number'");
$result = mysqli_query($database, $sql1) or die(mysqli_error($database));
echo '<table>';
echo '<th>Step</th><th>Url</th><th>Pass/Fail</th><th>Comment</th>';
while($fetch=mysqli_fetch_object($result))
{
echo '<tr>';
echo '<td>'.$fetch->step.'</td>';
echo '<td>'.$fetch->url.'</td>';
echo '<td>'.$fetch->pass_fail.'</td>';
echo '<td>'.$fetch->comment.'</td>';
echo '</tr>';
}
echo '</table>';
I currently have this code which displays all required information on the page:
$sql = "select * from livecalls ORDER BY Completion_Date ";
$query = mysql_query( $sql );
while( $row = mysql_fetch_assoc($query) )
{
echo "<tr><td>$row[ID]</td>";
echo "<td>$row[Type]</td>";
echo "<td>$row[VNC_Number]</td>";
echo "<td>$row[Completion_Date]</td>";
echo "<td>$row[Logged_By]</td></tr>";
}
echo "</table>";
This works fine, however I am wanting to be able to click the $row[ID] section to open a new window and display the $row[Problem] which is related to that ID number..
I'm struggling to think how to get the ID information across to a new page to be able to search for the right Problem information to display and the code to do this?
Any help would be appreciated.
You can use a anchor tag for this purpose like
echo "<tr><td><a href='yourpage.php?id=".$row[ID]."'>".$row[ID]."</a></td>";
and in new page, you can use
$_GET['id'] for getting the id.
Also while echoing html along with php variable try to do like this:
while( $row = mysql_fetch_assoc($query) )
{
echo "<tr><td><a href='yourpage.php'>".$row['ID']."</a></td>";
echo "<td>".$row['Type']."</td>";
echo "<td>".$row['VNC_Number']."</td>";
echo "<td>".$row['Completion_Date']."</td>";
echo "<td>".$row['Logged_By']."</td></tr>";
}
I guess your not using any php framework (ex. codeigniter). Because if you do,this is easy using routing.
The very basic solution is Passing variables in a URL
http://html.net/tutorials/php/lesson10.php
Then if you want it to open in a new window instead of current window use
Use _blank in your a href
Visit W3Schools!
$sql = "select * from livecalls ORDER BY Completion_Date ";
$query = mysql_query( $sql );
while( $row = mysql_fetch_assoc($query) )
{
echo "<tr><td><a href='your_url/?id=".$row[ID]."'>$row[ID]</a>`enter code here`</td>";
echo "<td>$row[Type]</td>";
..
...
....
I have the following code that works by outputting as a link ( the link comes from a field in my database) I wish to do the same for the code below, however i cannot get it work, here is the example of what I have that works, and the code that i wish to make output as a link:
Working Code what I want it to look like
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM adrenaline WHERE title LIKE '%".$term."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)){
echo '<br> '. $row['title'] .'';
}
}
?>
And the code that i have at the moment, it works by be manually typing in the hyper link, however I wish to make it take the link from the database like the example above
//query the database
$query = mysql_query("SELECT * FROM hobby WHERE id = '1' ");
//ferch the results / convert results into an array
WHILE($rows = mysql_fetch_array($query)):
$title = $rows['title'];
echo "<a href='shard.php'>$title</a>";
endwhile;
?>
Many thanks!
I am not 100% certain if this is what you meant to ask... let me know in comments:
<?PHP
$query = mysql_query("SELECT * FROM hobby WHERE id = '1' ");
if(mysql_num_rows($query) >= 1) {
while($rows = mysql_fetch_array($query)) {
echo sprintf("%s", $rows["description"], $rows["title"]);
}
} else { echo "No hobbies found."; }
?>
I believe you might have faced some syntax issues while dealing with quotes parsing a variable in <a html tag. Consider using sprintf something like in my example.
I have also added a mysql_num_rows() just in case and you can see its a good fail-safe method incase there are no rews found on any select query.
IMPORTANT: STOP using mysql_ functions because its deprecated from new PHP versions. Use PDO or mysqli instead.
I am designing an event feed from a calender I made. I'm using the same data from the database but to match specific dates and times.
I only want 4 events to show at once (why I specified length < 4)
Where the database value 'showFeed' is true, it only displays those rows.
and I want it to show by date time, I have odd id's for each value in the database, which might make them out of order.
My current code:
$sql = "SELECT `title`, `time`, `start`, `showFeed` FROM calender WHERE length('column') > '0'";
$result = $dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);
echo "<div class=\"eventfeed\">";
echo "<ul>";
foreach ($result as $row){
$show = $row['showFeed'];
if ($show == 1 && length.$result < 4){
echo "<li>";
echo $row['title']. "<br />";
echo $row['start'].' '.$row['time'];
echo "</li>";
}
else {
return false;
}
}
echo "</ul>";
echo "</div>";
$dbh = null;
echo json_encode($return);
?>
I'm getting results and no errors from the database, but I'm only seeing one return on $results.
I honestly, do not have a clue where else to go from here. I'm lost.
For 1+.2.+3. modify your query to SELECT title, time, start, showFeed FROM calender WHERE length('column') > '0' and showFeed=1 and time<current_timestamp ORDER BY time DESC LIMIT 0,3 and remove your if (...) statement.
I don't know if this is your actual code, but you should determine the length of an array by doing $result.length instead of the other way around.
Also, you can limit the number of results in your query using 'LIMIT 4' at the end of your query. That way MySQL only returns 4 results and you don't have to worry about that in your code, just print everything.
I need some help with some PHP and MySQL code. At the moment I have a php file which displays the contents of a table in my database. I would like it to omit results that match "Not Booked". So the file only shows me slots which are booked. Here is my code:
<link href="../css/pagestyle.css" rel="stylesheet" type="text/css" />
<?php
include("../config.php");
include("functions.php");
if($logged["level"] == "HDJ" OR $logged["level"] == "SA") {
echo "<div class=\"head\">View Booked Slots</div>
<img src=\"../images/spacer.png\" width=\"5\" height=\"5\">
<div class=\"board\">Here you can view booked slots.</div>
<img src=\"../images/spacer.png\" width=\"5\" height=\"5\">
<table width=\"580px\" class=\"board\" border=\>";
$order = "SELECT * FROM timetable";
$result = mysql_query($order);
while ($row=mysql_fetch_array($result)){
echo ("<tr><td>$row[username]</td>");
</tr>");
}
echo "
</table>
";
} else {
echo ("<div class=\"caution\">Access is denied.</div>");
}
?>
Modify the query not to pull them in the first place.
$order = "SELECT * FROM timetable WHERE <the column> <> 'Not Booked';
Replace <the column> with the correct column name in your table where Not Booked appears.
It is often not advisable to intermix database logic and display logic as you have done here. Instead, you ought to do the query before outputting your table, and store its results in an array. Then loop over the array to display your table.
$order = "SELECT * FROM timetable WHERE somecolumn <> 'Not Booked'";
$result = mysql_query($order);
// Error checking
if (!$result) {
// output error, take other action
}
else {
while ($row=mysql_fetch_array($result)){
// Append all results onto an array
$rowset[] = $row;
}
}
Later, loop over the array to output your values. Dont' forget to escape it for HTML output with htmlspecialchars().
foreach ($rowset as $row) {
echo "<tr><td>" . htmlspecialchars($row['username']) . "</td></tr>";
}
In your mySQL code:
SELECT * FROM timetable WHERE myvariable <> 'Not Booked'
Michael's answer will only pull out results that MATCH 'Not Booked' rather than omit them. Tweak the code to:
$order = "SELECT * FROM timetable WHERE <the column> <> 'Not Booked';
And you'll get all the booked ones (assuming there are only two states)