I have the following code in my program that works perfectly with the WAMP server. But I had to change the WAMP server for XAMPP and I don’t know why it doesn’t execute some parts of the code. There’s also no error message.
The problem is that in the table code <table>, it doesn’t show after the third <tr> or when the 'Toggle' starts. It also doesn’t show the submit button.
I don’t understand why everything works correctly in WAMP but not in XAMPP. Can someone help me see why it fails? Thank you
Project.php
<?php
include_once("Conexion.php");
include_once("functions.php");
ob_start();
$_SESSION['ex_time'] = date('Y-m-d H:i:s');
$list_ex = array();
$result = doSearch($conn);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="styleProject.css">
</head>
<body>
<div id="centered_A">
<form action='' method='post'>
<table id="exercises">
<tr>
<th colspan="3"><h2>Exercises</h2></th>
</tr>
<tr>
<th><h3>Exercise_ID</h3></th>
<th><h3>Title</h3></th>
<th><h3>Difficulty
<form id="difficulty_form" method="get">
<select id="filter" name="filter" onchange="document.getElementById('difficulty_form').submit()">
<option value="All">Todas</option>
<?php
for ($i = 1; $i <= 5; $i++) {
$selected = '';
if (isset($_GET['filter']) && $_GET['filter'] == $i) {
$selected = ' selected';
}
echo "<option value=\"$i\"$selected>$i</option>\n";
}
?>
</select>
</form>
</h3></th>
</tr>
<?php
$order = 1;
$num_ex = 1;
while($row = $result->fetch_assoc())
{
array_push($list_ex, $row["exercise_id"]);
$sql_check_ex = "select * from answers a where a.student_id=".$_SESSION['user_id']." and a.exercise_id_fk=".$row["exercise_id"].";";
$result_check_ex = $conn->query($sql_check_ex);
?>
<tr>
<td><?php echo $num_ex; ?></td>
<!---Click Toggle Exercise-->
<td><a onclick="myFunction(<?php echo $row["exercise_id"] ?>)" role="button" class="btn" target="_blank" ><?php echo $row["title"]; ?></a>
</td>
<td><?php echo $row["difficulty"]; ?></td>
</tr>
<!--- Toggle --->
<tr id="toggle<?php echo $row["exercise_id"] ?>" style="display:none">
<td colspan="3">
<?php
$sql = "SELECT * FROM exercises WHERE exercise_id='".$row["exercise_id"]."'";
$result_ej = $conn->query($sql);
$row_ej = $result_ej->fetch_assoc();
?>
<p><?php $row["exercise_id"] ?> <?php echo $row["text"]?></p>
<br><!--- Radio Button --->
<?php
if ($row["type"] == 0) {
$ansarray = explode("\n", $row["image_path"]);
$randomans = [];
for($i=0; $i<count($ansarray); $i++) {
$a = array($i+1, $ansarray[$i]);
array_push($randomans, $a);
}
shuffle($randomans);
for($i=0; $i<count($randomans); $i++) {
echo "<div class=\"radio\" style=\"text-align:left; display:flex; vertical-align: baseline;\">";
echo "<input type=\"radio\" style=\"margin-top: 8px; cursor:pointer;\" name=\"choice".$row["exercise_id"]."\" value= \"".$randomans[$i][0]."\" />".$randomans[$i][1]."<br>";
echo "</div>";
}
echo "<input type=\"text\" name=\"choicetext".$row["exercise_id"]."\" value='multi' style=\"display:none;\">";
} else {
?>
<input type="radio" style="margin-top: 8px" name="choice<?php echo $row["exercise_id"] ?>" value= "0" checked="checked" style="display:none;" />
<?php
}
?>
<br>
<!--- Select level --->
<p2>Select difficulty level:</p2>
<select name="choose<?php echo $row["exercise_id"] ?>" id="choose<?php echo $row["exercise_id"] ?>">>
<option value="1" <?php if($row["difficulty"]=="1") { echo "selected"; } ?> >1</option>
<option value="2" <?php if($row["difficulty"]=="2") { echo "selected"; } ?> >2</option>
</select>
</td>
</tr><!---Finish Toggle --->
<?php
$order++;
$num_ex++;
}
?>
</table>
<br><!---Submit Button-->
<input class="buttonSubmit" type="submit" name="submit_proj_ex" value="Submit">
</form>
<script>
function myFunction(ejer_id) {
var x = document.getElementById("toggle" + ejer_id);
if (x.style.display === "none") {
x.style.display = "table-row";
} else {
x.style.display = "none";
}
}
</script>
</div>
</body>
<?php
if (isset($_POST['submit_proj_ex'])) {
include_once("store_answers.php");
header('location: Results.php');
}
?>
</html>
functions.php
<?php
function doSearch($conn, $fields = "*") {
$sql = "SELECT $fields FROM exercises, ex_tag, tags where exercise_id = exercise_id_fk and tag_id = tag_id_fk";
if (isset($_GET['filter']) && $_GET['filter'] !== 'All') {
$filter = (int) trim($_GET['filter']);
$sql .= " and exercises.difficulty = '$filter'";
}
if (!empty($_SESSION['tags_array'])) {
$sql .= " and (";
foreach ($_SESSION['tags_array'] as $tagId)
$sql .= 'tag_id = ' . $tagId . ' or ';
$sql .= "tag_id = -1)";
}
$sql .= ' group by exercise_id_fk';
return $conn->query($sql);
}
?>
Run phpinfo() in both WAMP and XAMP. I'm not certain what your issue is, but if it works in one and not the other, perhaps they are using different versions of php. If this is the case, you will need to use the same version of php in your XAMP as you were using in WAMP, or go through your code and try to figure out how to convert your code, such that it works correctly with the version on your XAMP. The former is probably the path of least resistance.
Related
I have modified function.php in th wordpress and called php page from the wordpress menu using shortcode. I can able to connect and display php page.
In the php page able to display all the records intially without selecting value from dropdown list. But when I change the value in dropdown list instead of showing the filtered records depending on the dropdown list the page goes to wordpress index page. I suspect onaction command as a submit its not staying in the called php page instead it goes to the wordpress index page.
<?php
$selected = '';
function get_options($select) {
$categories = ['Select Category' => 0, 'Information Technology' => 1, 'Management' => 2];
$options = '';
while (list($k, $v) = each($categories)) {
if ($select == $v) {
$options .= '<option value="' . $v . '" selected>' . $k . '</option>';
} else {
$options .= '<option value="' . $v . '" >' . $k . '</option>';
}
}
//var_dump($options);
//echo var_dump($options)."<br>";
return $options;
}
require_once('dbconnect.php');
if (isset($_POST['categories'])) {
$selected = $_POST['categories'];
echo $selected;
}
if ($selected == 1) {
$selectedcat = 'Information Technology';
$selectsql = "SELECT * FROM courses where ccategory='$selectedcat'";
} else if ($selected == 2) {
$selectedcat = 'Management';
$selectsql = "SELECT * FROM courses where ccategory='$selectedcat'";
} else {
$selectsql = "SELECT * FROM courses";
}
//require_once('dbconnect.php');
//include('header-basic-light.php');
//$selectsql="SELECT * FROM courses";
$res = (mysqli_query($con, $selectsql));
if (!mysqli_query($con, $selectsql)) {
die(mysqli_error($con));
}
mysqli_close($con);
//header('Location:index.php');
?>
<HTML>
<head>
//<title>"View Information"</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="categories">Select the Category : </label>
<select name="categories" style="width:250px;" onchange="this.form.submit();">
<?php echo get_options($selected); ?>
</select>
</form>
<h2>View Information</h2>
<table class="table">
<tr>
<th>#</th>
<th>cname</th>
<th>start_date</th>
<th>duration</th>
<th>Remarks</th>
<th>Options</th>
</tr>
<?php
while ($r = mysqli_fetch_assoc($res)) {
?>
<tr>
<td><?php echo $r['cno']; ?></td>
<td><?php echo $r['cname']; ?></td>
<td><?php echo $r['start_date']; ?></td>
<td><?php echo $r['duration']; ?></td>
<td><?php echo $r['remarks']; ?></td>
<?php if ($r['ccategory'] == 'Information Technology') {
$catnum = 1;
}
if ($r['ccategory'] == 'Management') {
$catnum = 2;
} ?>
<td>Details  
</tr>
<?php } ?>
</table>
</body>
</html>
The usage of a short code is a requirement? Why not creating a page-slug.php, override it via a child theme and getting redirections and urls via standard permalink function?
Listbox shows blank line space below each element. Increased the width also but still the blank line appears. I have attached the screenshot for reference.I have attached the code file. I am using a function to select the records from the dropdown list . The dropdown list is working but blank record shows below every value which is shown in the screenshot.
<?php
$selected = '';
function get_options($select) {
$categories = array('Information Technology' => 1, 'Management' => 2);
$options = '';
while (list($k, $v) = each($categories)) {
if ($select == $v) {
$options .= '<option value="' . $v . '" selected>' . $k . '<option>';
} else {
$options .= '<option value="' . $v . '" >' . $k . '<option>';
}
}
return $options;
}
require_once('dbconnect.php');
if (isset($_POST['categories'])) {
$selected = $_POST['categories'];
echo $selected;
}
if ($selected == 1) {
$selectedcat = 'Information Technology';
$selectsql = "SELECT * FROM courses where ccategory='$selectedcat'";
} else
if ($selected == 2) {
$selectedcat = 'Management';
$selectsql = "SELECT * FROM courses where ccategory='$selectedcat'";
} else {
$selectsql = "SELECT * FROM courses";
}
//require_once('dbconnect.php');
include('header-basic-light.php');
//$selectsql="SELECT * FROM courses";
$res = (mysqli_query($con, $selectsql));
if (!mysqli_query($con, $selectsql)) {
die(mysqli_error($con));
}
mysqli_close($con);
//header('Location:index.php');
?>
<HTML>
<head>
<title>"View Information"</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="categories">Select the Category : </label>
<select name="categories" style="width:250px;" onchange="this.form.submit();">
<?php echo get_options($selected); ?>
</select>
</form>
<h2>View Information</h2>
<table class="table">
<tr>
<th>#</th>
<th>cname</th>
<th>start_date</th>
<th>duration</th>
<th>Remarks</th>
<th>Options</th>
</tr>
<?php
while ($r = mysqli_fetch_assoc($res)) {
?>
<tr>
<td><?php echo $r['cno']; ?></td>
<td><?php echo $r['cname']; ?></td>
<td><?php echo $r['start_date']; ?></td>
<td><?php echo $r['duration']; ?></td>
<td><?php echo $r['remarks']; ?></td>
<?php
if ($r['ccategory'] == 'Information Technology') {
$catnum = 1;
}
if ($r['ccategory'] == 'Management') {
$catnum = 2;
}
?>
<td>Details  
</tr>
<?php
}
?>
</table>
</body>
</html>
Listbox screenshot
1st : You missed forward slash in option close tag </option>
$options.='<option value="'.$v.'" selected>'.$k.'</option>';
2nd : Try to use prepared statement to avoid sql injection .
I want to make 4 columns in table. In code all the images are comes in single row and single column. But I want a single row containing 4 columns with 4 images (images fetching from database), then create another row and automatically add next 4 images & so on. I don't know how I do this can anyone please suggest me how I do this.
<form name="form">
<select id="sorting" style="width:140px" onChange="optionCheck()">
<option id="s">---Sort By----</option>
<option value="bydate">Sort By Date</option>
<option value="bytopic">Sort By Topic</option>
</select>
</form>
<br />
</div>
<?php include 'connection.php'; ?>
<div id="showByDefault">
<table style="width:60%">
<tr>
<?php include 'connection.php'; ?>
<div id="showByDefault">
<!--<table style="width:60%"><tr>-->
<?php
$sql1=mysqli_query($con,"select * from `insert-n-retrive-pdf` ORDER BY date DESC") or die(mysqli_error($con));
$i=0;
echo "<table><tr>";
while($row=mysqli_fetch_array($sql1))
{
if($i != 0 && $i%4 == 0) {
echo '<tr></tr>';
}
?> <td style="padding:20px;">
<img src="<?php echo $row["thumbnails"]; ?>" /></td><?php
echo '</tr>';
$i++;
}
?></tr></table>
</div>
<div id="hideall">
<div id="topic1">
<?php include 'pdf-sort-by-topic.php'; ?>
</div>
<div id="topic">
<?php include 'pdf-sort-by-date.php'; ?>
</div>
</div>
Try this one 100% working: Nice and easy.
<?php
$sql1=mysqli_query($con,"select * from `insert-n-retrive-pdf` ORDER BY date DESC") or die(mysqli_error($con));
$i = 0;
echo "<tr>";
while($row=mysqli_fetch_array($sql1)) {
if($i != 0 && $i%4 == 0) {
echo "</tr><tr>";
}
?>
<td style="padding:20px;"><img src="<?php echo $row["thumbnails"]; ?>" /></td>
<?php
$i++;
}
?>
Hope this helps!
You can try this code
$query = mysql_query("SELECT * FROM insert-n-retrive-pdf ORDER BY date DESC");
echo '<table width="960">';
$i = 0; //first, i set a counter
while($fetch = mysql_fetch_assoc($query)){
//counter is zero then we are start new row
if ($i==0){
echo '<tr>';
}
//here we creating normal cells <td></td>
$image_name = $fetch['thumbnails'];
$image_location = $fetch['path'];
echo '<td>'.'<img src="'.$image_location.'" alt="'.$image_name.'"/>'.'</td>';
//there is a magic - if our counter is greater then 4 we set counter to zero and close tr tag
if ($i>4){
$i=0;
echo '</tr>';
};
$i++; //$i = $i + 1 - counter + 1
}
echo '</table>';
You can fetch all your images into one-dimensional array and then use function array_chunk(). It will split an array into smaller parts you need. Here's a manual page.
Actually, You can get something like this:
<?php
$images = array();
while($row=mysqli_fetch_array($sql1))
{
$images[] = $row;
}
$images = array_chunk($images, 4);
?>
<?php foreach($images as $imagesChunk): ?>
<tr>
<?php foreach ($imagesChunk as $image): ?>
<td style="padding:20px;">
<a href="<?=$image["path"];?>" target="_blank">
<img src="<?=$image["thumbnails"];?>" />
</a>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
<table style="width:60%">
<tr>
<?php
$counter = 0;
$sql1 = mysqli_query($con, "select * from `insert-n-retrive-pdf` ORDER BY date DESC"
) or die(mysqli_error($con));
while($row=mysqli_fetch_array($sql1))
{
?>
<td style="padding:20px;">
<a href="<?php echo $row["path"]; ?>" target="_blank">
<img src="<?php echo $row["thumbnails"]; ?>" />
</a>
</td>
<?php
if($counter == 4)
{
echo "</tr>";
echo "<tr>";
$counter = 0;
}
$counter++;
}
?>
</tr>
</table>
In a site I'm making, I need a search engine to find songs for people to listen to. I have it working to the point that it can get info from the database and display them on the page. The problem comes when there are two songs with the same name. I have a system so the results will go to separate links, but when I search them they display the same image even though there is two separate sources for them. It also will make extra results for some reason. Here's my code:
<?php
if (isset($_GET['q'])) {
$q = $_GET['q'];
mysql_connect('********', '********', '********');
mysql_select_db('********');
$query = mysql_query("SELECT * FROM ******** WHERE title LIKE '$q'");
$numrows = mysql_num_rows($query);
} else {
echo "
<span style='font-family: Helvetica, Arial;font-weight: bold;font-size: 25px;'>Search</span>
<form action='http://www.example.com/search' method='get'>
<input placeholder='Search for music' type='text' name='q' style='font-weight:bold;padding:5px;width:300px;border-top-left-radius: 4px;border-top-right-radius: 10px;border-bottom-left-radius: 10px;border-bottom-right-radius: 4px;border: 3px solid gray;background-color:#000000;color:#FFFFFF;' />
</form>
";
}
if ($numrows != 0) {
$index = 0;
$results = array();
while($row = mysql_fetch_assoc($query)) {
$results[$index] = $row;
$index++;
foreach ($results as $result) {
$url = "http://www.example.com?id=" . $row['id'];
$title = $row['title'];
$arturl = $row['art_url'];
if ($_GET['q'] != "") {
echo "
<a href='$url'>
<table>
<tr style='text-align:left;'>
<td><img src='$arturl' style='width:100px;height:100px;'></td>
<td>
<span class='songTitle'>$title</span>
<br/>
<span class='songArtist'>By: Unknown</span>
</td>
</tr>
</table>
</a>
<br />
";
}
}
}
} else {
if ($_GET['q'] != "") {
echo "
<span style='font-family: Helvetica, Arial;font-weight: bold;font-size: 25px;'>Search</span>
<form action='********' method='get'>
<input placeholder='Search for music' type='text' name='q' style='font-weight:bold;padding:5px;width:300px;border-top-left-radius: 4px;border-top-right-radius: 10px;border-bottom-left-radius: 10px;border-bottom-right-radius: 4px;border: 3px solid gray;background-color:#000000;color:#FFFFFF;' />
</form>
";
echo "<br />No results where found.";
}
}
?>
while($row = mysql_fetch_array($result))Try $result['title'] instead of $row['title'];. Same goes to $url and $arturl
This should work.
if ($numrows != 0) {
$index = 0;
$results = array();
while($row = mysql_fetch_array($query)) {
$url = "http://www.example.com?id=" . $row['id'];
$title = $row['title'];
$arturl = $row['art_url'];
if ($_GET['q'] != "") {
echo "
<a href='$url'>
<table>
<tr style='text-align:left;'>
<td><img src='$arturl' style='width:100px;height:100px;'></td>
<td>
<span class='songTitle'>$title</span>
<br/>
<span class='songArtist'>By: Unknown</span>
</td>
</tr>
</table>
</a>
<br />
";
}
}
}
your codes seem right. However, to clarify, try to echo $arturl and see if it is getting the right source name. And also check the value in the database. Last thing is try to check whether the source image is in the correct folder. Try and give the feedback.
I want to have the ability to show all the entries in a database table and by each one give the user the ability to delete specific ones.
I am currently using a for each loop that loops through the database showcasing each entry.
$result = mysql_query("SELECT * FROM KeepScores");
$fields_num = mysql_num_fields($result);
echo "<table><tr>";
// printing table headers
echo "<td>Recent Posts</td>";
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
How would to add a delete button that appears by each one and removes the entry from the database table?
You can do it with forms:
//main.php
<?php $result = mysql_query("SELECT * FROM KeepScores"); ?>
<table>
<tr>
<td>Recent Posts</td>
</tr>
<?php while($row = mysql_fetch_array($result)) : ?>
<tr>
<td><?php echo $row['field1']; ?></td>
<td><?php echo $row['field2']; ?></td>
<!-- and so on -->
<td>
<form action="delete.php" method="post">
<input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<?php endwhile; ?>
</table>
//delete.php:
<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
header('Location: main.php');
}
Or you can do it with jQuery and AJAX:
//main.php
<?php $result = mysql_query("SELECT * FROM KeepScores"); ?>
<table>
<tr>
<td>Recent Posts</td>
</tr>
<?php while($row = mysql_fetch_array($result)) : ?>
<tr id="<?php echo $row['id']; ?>">
<td><?php echo $row['field1']; ?></td>
<td><?php echo $row['field2']; ?></td>
<!-- and so on -->
<td>
<button class="del_btn" rel="<?php echo $row['id']; ?>">Delete</button>
</td>
</tr>
<?php endwhile; ?>
</table>
<script>
$(document).ready(function(){
$('.del_btn').click(function(){
var del_id = $(this).attr('rel');
$.post('delete.php', {delete_id:del_id}, function(data) {
if(data == 'true') {
$('#'+del_id).remove();
} else {
alert('Could not delete!');
}
});
});
});
</script>
//delete.php
<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
$result = mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
if($result !== false) {
echo 'true';
}
}
It's all untested and sure needs some adjustment for your specific project, but I think you get the idea and I hope it helps.
Next time, please post your schema if you ask stuff about database.
I thought I would improve on this a little bit by wrapping the delete post in a class and function. I was having the same problem. and this worked great for me. Thanks again # Quasdunk
<?php
// Class to hold the remove post function
class someClass{
//Function for removing the post
function removePost(){
if(isset($_POST['delete_id']) && (!empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
$result = mysql_query("DELETE FROM post WHERE post_id='".$delete_id."' AND post_member='" . $_SESSION['SESS_USER'] . "'");
if($result !== false) {
echo 'true';
}
}
}
}
if(isset($_SESSION['SESS_MEMBER_ID'])){
$member = $_SESSION['SESS_USER'];
$res = mysql_query("SELECT * FROM post WHERE post_member='$member' ORDER BY timestamp DESC") or die(mysql_error());
$i = new someClass;
while($row = mysql_fetch_array($res)){
echo '<div style="width:100%;margin:0 auto;border-top:thin solid #000;">';
echo '<div style="width:600px;margin:0 auto;padding:20px;">';
echo $row['post_text'] . '<br>';
$postID = $row['post_id'];
echo '<div style="border-top:thin solid #000;padding:10px;margin-top:5px;background-color:#CCC;">';
echo 'You posted this on: ' . $row['post_date'] . '#' . $row['post_time'];
echo '<div style="float:right;">
<form method="post" action="'. $i->removePost() .'">
<input type="hidden" name="delete_id" value="'.$row['post_id'].'" >
<input type="submit" value="Delete Post">
</form>
</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
}
?>
Produce a key to each table, using jquery,then link it to a php file which an accept the key and delete from the specific table (which also can be passed through jquery)