Here is what i want to achieve ; sending ID's through URL's and printing it.
index.html
ID 1
ID 2
receive.php
<?php
$id_q = $_GET['id'];
print "The parameters passed through URL are $id_q";
?>
This above code works perfectly, I'm not able to do this with a list of ID's printed with a php command.
The below code is used to print all the PID's in the DB.How do i make every PID printed clickable ?
When I add html tags inside PHP code it throws up an error.
print.php
$result = mysqli_query($con,"SELECT * FROM List");
while($row = mysqli_fetch_array($result))
{
echo $row['PID'];
}
edit-query.php
$pid_q=$_GET[pid];
echo $pid_q;
while($row = mysqli_fetch_array($result))
{
echo "<a href='receive.php?id=".$row['PID']."'>".$row['PID']."</a>";
}
If you want to add your own text to a variable or echo, quote it and separate the variable with a "."
echo ''.$row['PID'].'';
you should do that like this
How about...
echo '' . $row['PID'] . '';
I believe this is what you mean?
while($row = mysqli_fetch_array($result))
{
echo 'Print ID: ' . $row['PID'] . '';
}
while($row = mysqli_fetch_array($result))
{
echo "<p id=".$row['PID']." class='clickable'>" . $row['PID'] . "</p>";
}
$(document).ready(function(){
$("#clickable").click(function(){
$(this)...something...
});
});
This is a little something you can do using JQuery if you wanted each PID to do something other than refer to another location. It will listen on any with the clickable class.
Related
I have an query that shows data from db. I use while loop to display data. Problem is that I can call (echo) result only once.
Here is my code:
$ime_ = "SELECT * FROM `users` WHERE '" . ($_COOKIE['username']) . "' = user_username";
$ime_result = $mysqli->query($ime_);
and later in my html I use this result as:
<?php
if ($ime_result->num_rows > 0)
while($row = $ime_result->fetch_assoc()) {
echo "<h2>" . $row["Ime"] . "</h2>";
}
?>
This work ok, but I want to use this result to display many times in my html. And when copy while loop again later in html no result is given.
Store the string with data from while() into a variable, than apply echo to that variable as many times as you like..
$re_out ='';
if($ime_result->num_rows > 0){
while($row = $ime_result->fetch_assoc()) {
$re_out .="<h2>". $row["Ime"] ."</h2>";
}
}
echo $re_out;
//etc..
echo $re_out;
<?php
if ($ime_result->num_rows > 0)
$ime_result->data_seek(0); // seek to row no. 1
while($row = $ime_result->fetch_assoc()) {
echo "<h2>" . $row["Ime"] . "</h2>";
}
?>
Haven't run script. Try if it works.
// Page 1 - Code below works fine, but when I click the href link the
// variable I want is not sent to page 2.
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>
<td> ' . $row['recipe_name'] . ' </td>
</tr>';
$recipe_name = $row['recipe_name'];
}
$_SESSION['recipe_name'] = $recipe_name;
echo '</table>'; // Close the table
?>
// Page2 - Code below receives the variable from page 1, but only the //last one in the table and not the one I clicked.
include ('core/init.php'); // Connect to the database
$recipe_name = $_SESSION['recipe_name'];
echo "My recipes is: ".$recipe_name."<br>";
?>
Try something like this using a get request.Since the users can see/alter the data, this is not the safest way of doing this but will do the job. Sessions are not involved in this technique.
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr><td>
' . $row['recipe_name'] . ' </td></tr>';
}
echo '</table>';
?>
// Page2
<?php
$recipe_name = $_GET['recipe_name'];
echo "My recipes is: ".$recipe_name."<br>";
?>
You are doing wrong in your page 2
Instead of session use $_GET['']; To get value from url we use $_GET.
Like this:
$recipe_name = $_GET['recipe_name'];
I hope this would help you.
I'm making simple basketball stats plugin to wordpress, and I'm using dropdown list a lot. I wanted to make function but I don't know how to pass arguments to MySQL. Here's my code:
function dropDown($tab, $option, $text){
$result = mysqli_query($con,'SELECT * FROM tab');
while($row = mysqli_fetch_array($result)){
echo "<option value=\"";
echo $row['option'] . "\">" . $row['text'];
echo "</option><br>";
}
}
and I would like to use it like this:
dropDown("team", "team_id", "name");
I tried with different quotation marks, dots etc but it doesn't seem to work.
#edit
I know PHP syntax (some of it) and I know how to use it, but I don't know how to pass $variables to MySQL query, and that's my main problem.
try
function dropDown($team, $team_id, $name) {
// use both three var where you want
$result = mysqli_query($con,'SELECT * FROM team');
echo "<select>";
while($row = mysqli_fetch($result)){
echo "<option value=\"";
echo $row['team_id'] . "\">" . $row['name'];
echo "</option>";
}
echo "</select>";
}
dropDown("team", "team_id", "name");
This may be an easy fix, but I can't get my head around it .
Basically I've remade my blog using a database and php rather than wordpress.
I want my site to show the most recent post first, then i'm using PhP postback to alter it
So far it works, but i can't figure out a way for it to automatically go to the most recent one but then change after the postback.
<?php
$inId = $data[0];
//if (!empty($inId))
//{
//}
//else
//{
//$inId = $_POST['ID'];
//}
include 'Includes.php';
$blogPosts = GetBlogPosts($inId);
foreach ($blogPosts as $post)
{
echo "<div class='post'>";
echo "<h3>" . $post->title . "</h3>";
echo "<p2>" . $post->post . "</p2>";
echo "<span class='footer'>Posted By: " . $post->Author . " Posted On: " . $post- >datePosted . "</span>";
}
echo '<form name="myForm" action="Index.php" onsubmit="return validateFormStrings()" method="post">';
echo'<select name ="ID">';
$query4 = "SELECT * FROM Blogs ORDER BY ID ";
$result = mysql_query($query4);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo '<option value="'.$row['ID'].'">'.$row['Title'].'</option>';
echo '<br>';
}
echo'</select>';
echo '<input type="submit" value="Retrieve Posts">';
echo '<a href="Index.Php" ></a>';
echo '</form>';
?>
As you see i tried to fiddle with !empty and things but that's not really what im trying to do.
If i'm unclear let me know.
I almost need something like " if ( button !pressed) then its data[0] else its the postback
Thanks in advance
I found out how to do it using the isset() function.
if (isset($_POST['submit1']))
{
$inId = $_POST['ID'];
}
else
{
$inId = $data[0];
}
This checks if the submit button has doe the postback or not. Works a treat.
Gonna have a go at trying it with Ajax so I don't need the button at all ^^
i have a question in regards to php and css layers.
i have the following php code:
session_start();
// Retrieve all the data from the table
$SQL = "SELECT * FROM Exodus_planets WHERE login_id = $user[login_id] LIMIT 10";
$result = mysql_query($SQL);
//while ($db_field = mysql_fetch_assoc($result)) {
//print $db_field['planet_name'] . "<BR>";
//print $db_field['location'] . "<BR>";
while($row = mysql_fetch_array( $result )){
echo " Planet, ".$row['planet_name'];
echo " is located at System ".$row['location'];
echo "<br> ";
}
which correctly displays a word [Planet name] and a number [System] in sequence.
The above code displays the information in rows such as;
Planet Sun is located in system 35.
Planet Saturn is located in system 30.
i'm just trying to make this information display a look a little nicer. in a way so planet name shows up in the right of a background image container and system in another corner possibly colored.
....
How do i place the above code inside floating css container(s)?
Thank you.
As it seems to be a list of planets, I would use an HTML list. You can edit the css to have it look the way you want after.
echo ' <ul class="planetList"> ';
while($row = mysql_fetch_array( $result )){
echo '<li>';
echo " Planet, ".$row['planet_name'];
echo " is located at System ".$row['location'];
echo '</li> ';
}
echo '</ul>';
In CSS
ul.planetList {
display:block;
float:right;
background-image:url('yourBackground.jpg');
background-position:left;
background-repeat:repeat-y;
/* CSS 3 Only */
background-size:{length of your text}px 100%;
}
You could also use a table instead of the list. This way, you could get a background for the planet column, and another one for the located at sytem one.
echo ' <table> ';
echo ' <tr><th>Planet</th><th>System</th></tr>';
while($row = mysql_fetch_array( $result )){
echo '<tr>';
echo '<td class="planet">' . $row['planet_name'] . '</td>';
echo '<td class="system">' . $row['location'] . '</td>';
echo '</tr> ';
}
echo '</table>';
CSS :
table>tr.planet {
background-image:url('yourBackground.jpg');
background-position:left;
}
table>tr.system {
background-color:#CCFF00;
}
Php code should be inside tags. You can always close these tags and put some htmls tags
Example:
<div class="bla"><?php
my code
?></div>
Obviusly you can do everything in php
<?php
echo '<div class="bla">';
....phpcode....
echo '</div>';
?>
Hopefully this helps
You can do something like this to keep your code clean
<?php
session_start();
// Retrieve all the data from the table
$SQL = "SELECT * FROM Exodus_planets WHERE login_id = $user[login_id] LIMIT 10";
$result = mysql_query($SQL);
$planets = array();
while($row = mysql_fetch_array( $result )){
$planets[] = $row;
}
foreach($planets as $planet): ?>
<div class="prettyFloatyDiv">
Planet, <?php echo $planet['planet_name']?>
is located at System <?php echo $planet['location']?>
<br/>
</div>
<?php endforeach; ?>
If you have short tags enabled, you can replace every <?php echo with <?=