I have an original index.php file about Forum installed at a HOST.
I need to make a table with two columns: one column for Ads and another column for the Forum.
When I use directly:
<?php
echo "<table>";
echo "<tr>";
echo "<td width='20%'>";
$dir = 'images/';
if ($opendir = opendir($dir))
{
$file = 'lenovo-a1000.jpg';
$type = 'image/jpeg';
echo "<img src='$dir/$file' width='240' height='401'";
}
echo "</td>";
echo "<td width='80%'>";
........
........ original code of the Forum;
........
echo "</td>";
echo "</tr>";
echo "</table>";
?>
it raises error messages, but the table appears;
Then to avoid error messages I decided to make two functions:
<?php
function tbl_open()
{
echo "<table>";
echo "<tr>";
echo "<td width='20%'>";
$dir = 'images/';
if ($opendir = opendir($dir))
{
$file = 'lenovo-a1000.jpg';
$type = 'image/jpeg';
echo "<img src='$dir/$file' width='240' height='401'";
}
echo "</td>";
echo "<td width='80%'>";
}
........
........ original code of the Forum;
........
function tbl_close()
{
echo "</td>";
echo "</tr>";
echo "</table>";
}
?>
Now there is not error messages, but the table does not appear.
How to solve my Problem ?
You dont need to echo HTML in PHP. Simply close the PHP tag, write yout HTML and open the PHP tag again when you want it again. EX:
<table>
<tr>
<td width='20%'>
<?php
$dir = 'images/';
if ($opendir = opendir($dir))
{
$file = 'lenovo-a1000.jpg';
$type = 'image/jpeg'; ?>
<img src='<?php echo $dir . $file ?>' width='240' height='401' />
<?php } ?>
</td>
<td width='80%'>
<?php
........
........ original code of the Forum;
........
?>
</td>
</tr>
</table>
The solution is not to make a table into a forum's index.php, because this will raise warnings and errors about header().
My opinion now is the solution is to have a previous .php file which to start two iframes vertically suited FRAMES, for example (iframe width=80%) for the Forum and (iframe width=20%) for ads. I tested this - it works without any warnings and without any errors.
Related
Right guys here is my big ass question, I'm just a begginer and all of that so it's probably just a silly and easy thing but how from this code
<?php
session_start();
if(!isset($_SESSION['password']))
{
header('Location:index.php');
exit();
if ($_SESSION['logged_in']= 0)
{
header('Location:index.php');
exit();
}
}
?>
<?php include 'includes/header.php';?>
<div class="page_menu_box">
<ul>
<li>USER PROFILE</li>
<li>ABOUT US</li>
</ul>
</div>
<div class="page_content">
<table class="viewbook_table">
<tr>
<th>ISBN</th>
<th>AUTHOR</th>
<th>DATE WHEN PUBLISHED</th>
<th>COVER</th>
</tr>
<?php
include("core/database/connect.php");
$connection = #new mysqli('localhost','root','','bs_admin_tools');
$sql = "SELECT * FROM books";
$query = $connection->query( $sql );
while ($row = mysqli_fetch_array($query))
{
echo "<tr>";
echo "<td width = 15%>";
echo $row['ISBN'];
echo "</td>";
echo "<td width = 15%>";
echo $row['author'];
echo "</td>";
echo "<td width = 15%>";
echo $row['datepublished'];
echo "</td>";
//check whether an image is available for the record chosen
if ( $row['imgdir'] )
{
//create a variable which holds data from the images folder
$imageDir = "image/";
//create a second variable which holds the value of the image linked to the record selected
$img = $imageDir . $row['imgdir'];
/*link to the directory*/
echo "<td width ='15%' padding='30%'>";
//display the image
echo "<left><a href='viewdetails'><img src='$img'></a>";
echo "</td></tr>";
}
//echo "<br />"; //display a line break
}//end of the while loop
//release connection from database
mysqli_close($connection);
?>
</table>
</div>
<?php
echo "<div class='page_menu_box'><ul><li>
".$_SESSION['login']."
</ul></li></div>'";
include 'includes/footer.php';?>
I just want to basically click on the $img and view the book details, in other words I make one of the variables into a hyperlink and whenever someone is transferred onto another page I want a variable to be sended to that file where I would facilitate the file to intake the variable and display other shit about that book using 'Select from books where $theVariable="XD"'. I would be really happy if anyone could help me, I'm bouncing my head over a wall for the 2nd day to work that out.
I can only do stuff on php so please solutions only involving php or html ;_;
I have these codes and am not good at php please can someone help me on how to echo the videos to appear horizontall in my page thanks in advance
<?php
$h=2;
$k=mysql_query("SELECT * from aupload where type='audios' and view>='$h' order by view DESC");
while ($la=mysql_fetch_array($k)){
?>
<center>
<table width='100%'height=''>
<tr>
<?php echo"<td>" . "<a href='uploads/$la[filename]'><img src='uploads/$la[size]' width='180px' height='180px'>
<br><b>$la[filename]</b><br><a href='music1.php?id=".$la['id']."'><input type='button' value='DOWNLOAD'><input type='button' value='$la[view]'> </a></a>
"?></td>
</tr>
</table><br>
</center>
<?php }?>
<?php
include('connect.php');
$k=mysql_query("SELECT * from aupload where type='audios'");
#$la=mysql_fetch_array($k)
?>
<?php
$h=2;
$y=mysql_query("SELECT * from aupload where type='videos' and view>='$h' order by view DESC ");
?>
<table border='0' id='myTable'width='100%'>
<?php
while ($x=mysql_fetch_array($y)){
echo "<tr>";
echo "<td>" . "<video id='myVideo' onclick='message()' width='180px' height='180px' controls><source src='uploads/$x[filename]' '></video>" . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td width='' height=''>"."<a href='music2.php?id=$x[id]' > <b>$x[filename]</b><br><input type='button' value='DOWNLOAD'></a>" ."<input type='button' value='$x[view]'></a></a>". "</td>";
echo "</tr>";
}
echo "</table>"; echo"</center>";
?>
the problem is when I upload two or more videos they appear vertically but I just want them to appear horizontally when echo them please someone help and thanks
Use frames is the best way to display a video, try this code
This is actually a HTML question, not a PHP question. In HTML, designates a table row, and can contain many or table cell elements. So you want to have the a echoed before the while loop, then print just the elements in the while loop, then print the . This will put all the elements in a single table row.
I have problem with positioning html tags I did try to solve the problem but for me the code looks fine however the output tells something different I did try do to sort out the output of SQL query however I wasn't able to achieve this and when I did asked for help in the forum in reply I get that it would be much easier to do it in PHP. So I have produce following code but I am not getting required results:
<body>
<div class="container">
<?php
$sdate = '';
foreach($rows as $row) {
if($row['shieldDate'] != $sdate){
$sdate = $row['shieldDate'];
echo '<h2>';
echo $row['shieldDate'],' ','opponent',' ',$row['shieldTeam'];
echo '</h2>';
echo "
<p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>
<table class='table'>
<thead>
<tr>
<th>Player</th>
<th>Score</th>
</tr>
</thead>
<tbody>
";
echo "<tr>";
echo "<td>";
echo $row["firstname"],' ', $row["lastname"];
echo "</td>";
echo "<td>";
echo $row["score"];
echo "</td>";
echo "</tr>";
}else{
echo "<tr>";
echo "<td>";
echo $row["firstname"],' ', $row["lastname"];
echo "</td>";
echo "<td>";
echo $row["score"];
echo "</td>";
echo "</tr>";
}
}
?>
</tbody>
</table>
</div>
</body>
I am trying to acheive following output:
However I am getting:
You don't close your tbody and table tags between two dates.
Each time you encounter a new date, you open a new table tag, but you never close it before having a new h2 date title. Add a line to close it.
$sdate = '';
foreach($rows as $row) {
if($row['shieldDate'] != $sdate){
// It's a new date
if (!empty($sdate)) {
// It's not the first date: close previous table
echo '</tbody></table>';
}
$sdate = $row['shieldDate'];
echo '<h2>';
echo $row['shieldDate'],' ','opponent',' ',$row['shieldTeam'];
echo '</h2>';
echo "<p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>
<table class='table'>
<thead>[...]</thead>
<tbody>";
[...]
} else {
[...]
}
}
if (!empty($sdate)) {
// There has been at least one date (at least one table): close it
echo '</tbody></table>';
}
Note that the [...] content is the same in if and else instructions.
You could do this to have something cleaner. The less duplicate code you have, the more easy it is to read. The more easy to read your code is, the better.
foreach($rows as $row) {
if($row['shieldDate'] != $sdate){
// Do your h2 and open table stuff
}
echo "<tr>";
echo "<td>";
// etc. No need to put it in the if AND in the else instructions.
}
How to display the particular image in case if that image is not available in database? I have
database name:project
table name: image
fields: id (int), file (varchar) [image url stored here], name (varchar) [image description]
PHP code is here:
<?php
$con=mysql_connect("localhost","root","") or die("no connection ");
mysql_select_db("project")or die("no database exit");
echo "<h2 align='center'>Displaying image from database</h2>";
$res=mysql_query("SELECT * FROM image");
echo "<table>";
while ($row=mysql_fetch_array($res)) {
echo "<tr>";
echo "<td>";echo $row["id"];echo "</td>";
echo "<td>"; ?> <img src="<?php echo $row["file"]; ?>" height="100px" width="150px"> <?php echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "</tr>";
}
?>
</table>
simply use #getimagesize description w3school php.net this method will check if image actually exists . will return false if image deleted from db or from destination.
<?
$img="image url"; //orginal image url from db
if(!#getimagesize($img))
{
$img="default image" //if image not found this will display
}
?>
Update
for your code use like this
<?php
$con=mysql_connect("localhost","root","") or die("no connection ");
mysql_select_db("project")or die("no database exit");
echo "<h2 align='center'>Displaying image from database</h2>";
$res=mysql_query("SELECT * FROM image");
echo "<table>";
while ($row=mysql_fetch_array($res)) {
$img=$row["file"]; //orginal image url from db
if(!#getimagesize($img))
{
$img="default image" //if image not found this will display
}
echo "<tr>";
echo "<td>";echo $row["id"];echo "</td>";
echo "<td>"; ?> <img src="<?php echo $img; ?>" height="100px" width="150px"> <?php echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "</tr>";
}
?>
</table>
Use a simple if condition
<img src="<? php if($row["file"]){ echo $row["file"] ; } else{// other image name}?>" height="100px" width="150px">
You can use ternary operator to do this, refer below and give a try
<?php
$con=mysql_connect("localhost","root","") or die("no connection ");
mysql_select_db("project")or die("no database exit");
echo "<h2 align='center'>Displaying image from database</h2>";
$res=mysql_query("SELECT * FROM image");
echo "<table>";
while ($row=mysql_fetch_array($res)) {
// USE TERNARY OPERATOR HERE
$imagePath = (isset($row["file"]) && !empty($row["file"]) && file_exists($row["file"]))?$row["file"]:'img.default.jpg'; // REPLACE YOUR IMAGE PATHE HERE
echo "<tr>";
echo "<td>";echo $row["id"];echo "</td>";
echo "<td>"; ?> <img src="<?php echo $imagePath; ?>" height="100px" width="150px"> <?php echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "</tr>";
}
?>
</table>
If the file reference in the database is pointing to a file that may have been deleted, you have to check if the file still exists. Ideally, when he file is deleted, the reference in the database should be updated too, to avoid such problems, but that may not always be possible, especially when many people have access to FTP.
Check to see if file exists:
if(file_exists($yourImage)){
echo $yourImage;
}
else{
echo $defaultImage;
}
You're storing the directory to the image within the varchar? if so try changing your database to store the image itself using BLOB as explained here
Then you can use an if statement.
<img src="<? php if($row["file"]){echo $row["file"];} else { echo '/directory/to/image.jpg'}?>" height="100px" width="150px">
I would also recommend using a include, this may not be 100% relevant for your current project but can help down the line when needing to use more than one database connection.
create a dbconnect.php file //name it what ever you want
insert code into dbconnect.php file:
<? php $con=mysql_connect("localhost","root","") or die ("no connection"); mysql_select_db("project")or die("no database exit");?>
in any file you're trying to use a database use include_once 'dbconnect.php'
<?php
# session
session_start();
# check that session is set and is valid
if(!isset($_SESSION['login']))
{ header('Location: login.php');
}
?>
<body>
<div class="maincontainer">
<div class="keywordhead">
<div align="center"><img src="Images/keyword_title.png" width="243" height="56" /></div>
</div>
<div class="results">
<p>
<?php
$kword = $_POST["kword"];
function boldText($text, $kword) {
return str_replace($kword, "<strong>$kword</strong>", $text);
}
$testin1 = substr($kword,0,1);
if($testin1 == "") {
print "<strong>No Keyword or a Keyphrase Entered, Please return to the '<a href='keyword_search.php'>Keyword Search Page</a>'</strong>";
}
else {
// Connects to your Database
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
}
mysql_real_escape_string($kword);
$data = mysql_query("select company_name, section_name, question, answer from company, rfp, section, question_keywords
where company.company_id = rfp.company_id
and rfp.rfp_id = question_keywords.rfp_id
and question_keywords.section_id = section.section_id
and keywords like '%$kword%';")
or die(mysql_error());
echo "<table border=0 cellpadding=10>";
echo "<tr align = center bgcolor=white>
<td><b>Company Name</b></td><td><b>Section</b></td><td><b>Question</b></td><td><b>Answer</b></td>" ;
while($info = mysql_fetch_array( $data ))
{
echo "<tr>";
echo "<td width = 130px>".boldText($info['company_name'], $kword) . "</td> ";
echo "<td width = 60px>".boldText($info['section_name'], $kword) . " </td>";
echo "<td width = 300px>".boldText($info['question'], $kword) . " </td>";
echo "<td width = 600px>".boldText($info['answer'], $kword) . " </td></tr>";
}
echo "</table>";
?>
</p>
</div>
<div class="footer"><a href="logout.php"><br />
Logout</a> | Index | Back</div>
</div>
</body>
</html>
I am relatively new to PHP, and i was curious as to whether a certain function is possible. I have a keyword Search and the code for the results page is above. I would like to bold wherever the $kword variable appears on the page. is this possible?
Thanks
You can create a function to do so, and call it prior to echo'ing the variables.
Instead of: $info['question'] use boldText($info['question'], $kword)
function boldText($text, $keyword) {
return str_ireplace($keyword, "<strong>$keyword</strong>", $text);
}
As a side note, don't forget to escape $kword with mysql_real_escape_string() before using it in a SQL query, or even better, consider using MySQLi or PDO since mysql extension is strongly discouraged
Can you use something like this in each of your Print statements?
str_replace($kword, "<b>$kword</b>", $info[...])
(or CSS e.g.
<span style='font-weight:bold'>...</span>
if you prefer).