I have PHP coding that works but I have had no luck transferring this to a HTML form. Any advise?
<?php
$db_host = "localhost";
$db_username = "combsb_combsb";
$db_pass = "pat60086";
$db_name = "combsb_sample";
#mysql_connect ("$db_host", "$db_username", "$db_pass") or die ("Could not connect to MySQL");
#mysql_select_db("$db_name") or die ("No Database");
Echo"Successful Connection";
$sql = "SELECT compname FROM Crew";
$result = mysql_query($sql);
echo "<select name='compname'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['compname'] . "'>" . $row['compname'] . "</option>";
}
echo "</select>";
?>
First I'd make sure to add the HTML boilerplate around your PHP. Then I think you mean mysql_fetch_row instead of mysql_fetch_array. Your final file should look something like:
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
// *Cut out for brevity, remember to paste back in*
$result = mysql_query($sql);
?>
<form action="" method="POST">
<?php
echo "<select name='compname'>";
while ($row = mysql_fetch_row($result)) {
echo "<option value='" . $row['compname'] . "'>" . $row['compname'] . "</option>";
}
echo "</select>";
?>
</form>
</body>
</html>
Post your output / errors next time or if this doesn't work
Related
This is my pagination program :
if(isset($_POST['view']))
{
$per_page = 20;
if(isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 1;
}
$start = $per_page * $page;
$start = $start - $per_page;
if(isset($_GET['seller']) ) {
$seller = $_GET['seller'];
} else {
$seller = ($_POST['seller']);
}
echo $seller."_".$start;
$query = "SELECT Kala.mark ,Kala.sharhe_kala, Kala_forooshande.date From Kala Join Kala_forooshande ON Kala.id_kala=Kala_forooshande.id_kala WHERE Kala_forooshande.id_forooshande=(SELECT id_forooshande From Forooshande where forooshande='". $seller ."') LIMIT $start , $per_page";
$result = mysqli_query($db,$query);
$query2 = "SELECT COUNT(*) as total FROM Kala Join Kala_forooshande ON Kala.id_kala=Kala_forooshande.id_kala WHERE Kala_forooshande.id_forooshande=(SELECT id_forooshande From Forooshande where forooshande='". $seller ."')";
$result2 = mysqli_query($db,$query2);
if ($result->num_rows > 0) {
echo " <form method='post' enctype='multipart/form-data'>
<table class='blueTable'><thead><tr><th>شماره</th><th>شرح کالا</th><th>مارک</th><th>تاریخ</th></tr></thead>";
while ($row = $result->fetch_assoc()) {
$tarikh=gregorian_to_jalali((substr($row["date"],0,4)),(substr($row["date"],5,2)),(substr($row["date"],8,2)));
$j=$start++;
echo "<tr><td>" . $j . "</td><td>" . $row["sharhe_kala"] . "</td><td>" . $row["mark"] . "</td><td>" .$tarikh[0]."-".$tarikh[1]."-".$tarikh[2] . "</td></tr>";
}
echo "</table></form> ";
$total = mysqli_fetch_assoc($result2);
$total_page = (ceil($total['total'] / $per_page));
echo "<table class='pagination'>";
$prev = $page - 1;
if ($page <= 1) {
echo "
<td> << </td>
";
} else {
echo "
<td> << </td>
";
}
for ($i = 1; $i <= $total_page; $i++) {
if ($i == $page) {
echo "
<td class='active'>$i</td>";
} else {
echo "
<td>" . $i . "</td>";
}
}
$next = $page + 1;
if ($page >= $total_page) {
echo "
<td>>></td>
";
} else {
echo "
<td> >></td>
";
}
echo "</table>";
}
}
The problem is when the user click on page 2 , the <a> tag sends <a href=\"?page=" . $i . "&seller=".$seller."\">" to see the page number 2 , but it doesn't work because of the if clauseif(isset($_POST['view']) at the begining
So it just works for first page which the view button is clicked .
Is it possible to send button value in the link to simulate on button click event ?
Based on your comment on the question:
i want to be shown only if user click on view button
Then you want more view buttons. Replace your links with their own forms which post the values your server-side code expects. So instead of this:
echo "
<td>" . $i . "</td>";
You might have something like this:
echo "
<td>
<form method=\"post\">
<input type=\"hidden\" name=\"page\" value=\"" . $i "\" />
<input type=\"hidden\" name=\"seller\" value=\"" . $seller "\" />
<input type=\"submit\" name=\"view\" value=\"" . $i "\" />
</form>
</td>";
You can use CSS to style your buttons to look like links if you prefer. Tools like Boostrap make that very easy, just adding classes like "btn btn-link" to any clickable element for example.
But ultimately if you want your links to submit a form them make them forms.
I have a table in which I am printing a button at the end of each row. I wish this button to be small.
And my text is not getting truncated when the table is printed despite using overflow ellipsis. I don't want the horizontal scroll bar.
My title bar of the table, the topmost row is always getting hidden as I scroll down.
Any help is much appreciated. Here is my CSS script https://jsfiddle.net/v6sahfyc/1/
Edit: Adding the php code here:
// <?php
// echo "<div id='awesometable'>";
// ?>
<!-- <CAPTION>RESERVATION TABLE</CAPTION> -->
<?php
echo "<br><br><p style=\"font-family:verdana; font-size:100%; margin-left: 5em;\"><i><strong>Reservation Table.</strong></i></p>";
echo "<table id='reservation' class='inlineTable'>"; //style='border: solid 1px black;'
//echo "<thead>";
echo "<tr>";
echo "<th class=\"row-1 uid\">UserId</th>";
echo "<th class=\"row-2 rname\">Resource <br> Name</th>";
echo "<th class=\"row-3 rtype\">Resource <br> Type</th>";
echo "<th class=\"row-4 rloc\">Resource <br> Location</th>";
echo "<th class=\"row-5 treq\">Time <br> Required </th>";
echo "<th class=\"row-6 REM\">Delete? </th>";
echo "</tr>";
//echo "</thead>\n";
//echo "<col = width: 3em />";
?>
<?php
$username = "root";
$password = "";
$host = "localhost";
$connector = mysql_connect($host,$username,$password) or die("Unable to connect");
$selected = mysql_select_db("mydb", $connector) or die("Unable to connect");
$selectSQL = "SELECT user_ID, ResourceName, ResourceType, ResourceLocation, ReservationTime FROM reservation_table";
if( !( $selectRes = mysql_query( $selectSQL ) ) ){
echo 'Retrieval of data from Database Failed - #'.mysql_errno().': '.mysql_error();
}else{
?>
<tbody>
<?php
if( mysql_num_rows( $selectRes )==0 ){
}else{
while( $row = mysql_fetch_assoc( $selectRes ) ){
echo "<tr><td>{$row['user_ID']}</td><td>{$row['ResourceName']}</td><td>{$row['ResourceType']}</td><td>{$row['ResourceLocation']}</td><td>{$row['ReservationTime']}</td>";
$active = $_SESSION['usr'] == $row['user_ID'];
echo "<td>"."<button class='btn' type='submit' id='1'".($active ? "" : "disabled").">"."Delete"."</button>"."</td>";
}
}
?>
</tbody>
</table>
<?php
mysql_close($connector);
}
?>
I am trying to load articles from 1 to 5 on the homepage of this website.
When loading the homepage, only results from ID 2 to 5 vs. 1 to 5 are getting displayed and I'm not sure why. It seems there probably is a problem with my while loop but I can't seem to figure it out.
I have added a link to a screenshot of the database to show that there is in fact an article with the ID 1 and I've also added a link to the website itself.
Database in question
Website in question
<div class="bodymainwrap">
<div class="contentwrap">
<?php
if (isset($_GET['id']) && !empty($_GET['id'])) {
$page = "between " . (($_GET['id']*5)-5) . " and " . ($_GET['id']*5);
} else {
$page = "between 1 and 5";
}
require_once '/db.connect';
$query = "SELECT * FROM Articles where id " . $page;
$result = mysqli_query($link, $query);
if (!$result) {
echo "<br />" . $query;
die("<br/> Error: occured while trying to execute the query " . mysqli_error($link));
}
$row = mysqli_fetch_array($result)
?>
<div class="sidebar" align="center">
<a href="/nothing/index.php?id=2" >Page 2</a>
<?php echo $query ?>
</div>
<?php
while ($row = mysqli_fetch_array($result)){
echo '<div class="entry">';
echo '<img src="/nothing/images/julie.jpg">';
echo'<H2>';
echo $row['Title'];
echo'</h2>';
echo '<p>';
echo $row['Article'];
echo '</p>';
echo '</div>';
}
?>
</div>
Your issue is here I believe, with the lone mysqli_fetch_array call after you get $result
$result = mysqli_query($link, $query);
if (!$result) {
echo "<br />" . $query;
die("<br/> Error: occured while trying to execute the query " . mysqli_error($link));
}
$row = mysqli_fetch_array($result) //delete this
Here you have already fetched the first row. Remove that, and just keep it within the loop. That's the underlying reason for how the while loop works. It keeps advancing while mysqli_fetch_array doesn't return null.
View image: http://i.stack.imgur.com/yxE4u.jpg
Although this is in PHP, I'm trying to (with HTML) get two words (which use the same class) to push next to each other without calling the class seperately or using the two words in the same if/if else function. I'm sure it's just a simple HTML element.
<?php
if (something){
echo "<p class='message'>hello</p>";
}
if (somethingelse){
echo "<p class='message'>world</p>";
}
?>
As the comments aren't what I'm after, I'll show what I'm using:
<?php
if (!empty($message)){
echo "<p class='message'>" . $message . "</p>";
}
if (!empty($errors)){
echo "<p class='message'>";
foreach ($errors as $error){
echo " - " . $error . "<br />";
}
echo "</p>";
}
?>
One statement or both at the same time may prove true. Hopefully you can see why I can't wrap it all around an HTML tag.
i think you want to do this
<p class="messasge"> <?php echo $message; ?> </p>
RE-FIXED according to updated specifications :
<?php
if (!empty($message)){
echo "<p class='message'>" . $message;
}
if (!empty($errors)){
if (empty($message) echo "<p class='message'>";
foreach ($errors as $error){
echo " - " . $error . "<br />";
}
}
if ((!empty($message))||(!empty($errors))) echo "</p>";
?>
if JUST message is true it'll print <p class='message'>MESSAGE</p>
if JUST errors is true, it'll print <p class='message'>ERRORS</p>
if BOTH are true, it'll print <p class='message'>MESSAGEERRORS</p>
I think you probably want some variant on the following code:
<?php
$output = '';
if (something)
$output .= 'hello';
if (somethingelse)
$output .= 'world';
if ($output)
echo "<p class='message'>$output</p>";
?>
For your example, you could replace all of the echos in the first two conditions with appending to $message, then only output if there is something to output. There are numerous options but I think this is the most flexible / elegant.
In your case it can be simplified to,
<?php
$output = $message;
if (!empty($errors))
foreach ($errors as $error)
$output .= " - $error<br />";
if (!empty($output))
echo "<p class='message'>$output</p>";
?>
Maybe the dl list is useful
<?php
if (!empty($message)){
echo "<dl class='message'><dt>" . $message . "</dt>";
}
if (!empty($errors)){
foreach ($errors as $error){
echo "<dd> - " . $error . "</dd>";
}
echo "</dl>";
}
?>
<?php
$message = "";
if (something) $message = "hello";
if (somethingelse) $message += " world";
echo "<p class='message'>".$message."</p>";
?>
Here is my code:
<html>
<?php
DEFINE('DATABASE_USER', 'sfasdfasd');
DEFINE('DATABASE_PASSWORD', 'asdfasdfasdf');
DEFINE('DATABASE_HOST', 'sdfasdfasd');
DEFINE('DATABASE_NAME', 'dsafsdfasd');
$connect = mysql_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME) or
die ("Hey loser, check your server connection.");
mysql_select_db("minedb");
$query = "SELECT * FROM ideastable ORDER BY datee DESC";
$quey1="select * from ideastable";
$result=mysql_query($query) or die(mysql_error());
?>
<table border=1 style="background-color:#000000;" >
<caption><EM>Ideas List</EM></caption>
<tr>
<th>IDEAS</th>
<th>Thumbs Ups</th>
</tr>
<?php
while($row=mysql_fetch_array($result)){
echo "</td><td>";
echo $row['idea'];
echo "</td><td>";
echo $row['thumbsup'];
$i = $row['id'];
echo $i;
echo 'Thumbs Up!';
echo "</td></tr>";
}
echo "</table>";
?>
<SCRIPT type="text/javascript" src="jquery.min.js"></SCRIPT>
<SCRIPT type="text/javascript">
function doSomething() {
var myVar = "<?php echo $i; ?>";
alert(myVar);
$.load('uts.php?i=myVar');
return false;
}
</SCRIPT>
</html>
My question is how would I be able to make it so that when I click the Thumbs Up it recognizes the row the link was in? I am making a site where you can rate some of the objects in a database and that is the start up of it.
How about passing id to javascript function?
echo 'Thumbs Up!';