How to echo a list into a textarea? - php

I'm trying to echo a list into a textarea (to allow for simple copy and paste), I've got the list echoing out as flat html and its appearing as it should but I'd like it to be inside a textarea, here's what I've done so far...
<?php
include (ABSPATH.'/connect_include.php');
$AvDates = "SELECT * FROM DB_Available_Dates";
$AvDates = mysql_query($AvDates) or die(mysql_error());
if(mysql_num_rows($AvDates) > 0){
?>
<input value=
<ul>
<?php
while ($row_AvDates = mysql_fetch_assoc($AvDates)){
?>
<li><?php echo htmlentities($row_AvDates['Month']).' - '. htmlentities ($row_AvDates['the_days']); ?></li>
<?php
}
?>
</ul> />
<?php
}
?>
This isn't working though - how can I make this work?

I guess this might help you:
<textarea>
<?php
$anyVar = "This\nis\na\nstring";
echo $anyVar;
?>
</textarea>
Update: Demo for your example
<?php
include (ABSPATH.'/connect_include.php');
$AvDates="SELECT * FROM DB_Available_Dates";
$AvDates=mysql_query($AvDates) or die(mysql_error());
if(mysql_num_rows($AvDates)>0)
{
?>
<textarea>
<?php
while ($row_AvDates=mysql_fetch_assoc($AvDates))
{
echo htmlentities($row_AvDates['Month']) . " - " . htmlentities ($row_AvDates['the_days']) . "\n";
}
?>
</textarea>
<?php
}
?>

Related

Fixing my search form in HTML with embedded PHP?

So I have a PHP page which has an ordered list that I am scanning from a text file which looks like this:
Here is the code for it as well:
<?php
foreach (file("Files/music.txt") as $music){
list($title,$artist,$link) = explode(",",$music);
?>
<li>
<a href=<?php echo $link ?>><?php echo $title ?> by <?php echo $artist ?></a>
</li>
<?php } ?>
</ol>
</div>
What I need is to be able to search the title for the song and to only display that with the form I have in the picture. I have to use only the $_GET and a function to make it work. Here is the code for the form that I am messing around with:
<form action="blog.php">
Search for Title:
<input type="text" name="title">
<input type="submit" value="Search">
<br>
Sort by song title
Unsorted
<?php
function printSongs($searchTitles=""){
if(isset($searchTitles)){
global $title;
if ($searchTitle == $title){
echo $title;
}
}
}
$userInput = $_GET["title"];
printSongs($userInput);
?>
</form>
Any advice would help alot. Thank you!
try using The SplFileObject class in PHP try this code
<?php
function printSongs($searchTitles=""){
$file = new SplFileObject("Files/music.txt");
$music="";
while (!$file->eof()) {
$music= $file->fgets();
$data= explode(",",$music);
if (in_array($searchTitles, $data))
{
//match is found
echo data[1];
}
}
}
$userInput = $_GET["title"];
printSongs($userInput);
?>

How to echo PHP and HTML in if statement

I have an if statement that only has to show some code if the value of $result37 = ncrteam... but how can I echo that HTML and PHP code? echo" "; is not working and echo ' '; Is also not working.
This is my code:
<?php
if ($result37 === "ncrteam") {
?>
Status:<br>
<select class="form-control" name="status" style="width: 300px">
<?php
while ($row15 = mysqli_fetch_assoc($result15)):; ?>
<option selected value=\"<?php echo $row15['status'];?>\"><?php echo $row15['status'];?></option>
<?php endwhile;?>
<?php while ($row16 = mysqli_fetch_assoc($result16)):; ?>
<option value=\"<?php echo $row16['statusname'];?>\"><?php echo $row16['statusname'];?></option>
<?php endwhile;?>
</select>
<?php
}
?>
This is not a complete answer, because it is unclear what exactly you are trying to do, but your question looks something like How do I build html based on values of my php variables and I will try to answer it as best I can
With the limited information I have from your question, I think this is the kind of thing you are trying to do:
<?php
...
$html = "";
if ($result37 === "ncrteam") {
while ($row15 = mysqli_fetch_assoc($result15))
{
$html .= "<option value=".$row15['status'].">";
}
}
?>
<html>
<body>
...
<select>
<?php echo $html; ?>
</select>
...
</body>
</html>

Returning SQL data within an image tag using PHP

When I run the following file I get the database data i.e it prints it out on the website so I know my connections are good.
<html>
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo $row["name"], $row["image"];
}
?>
</div>
</html>
However when I try and format the results like below
<html>
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo <div id = "bookbar">
<img src= "$row['image']" alt = "image">
<p> $row['name'] </p>
</div>
}
?>
</div>
</html>
it doesn't work. Can anyone help me fix the code?
Maybe try this your code didn't close/open the php tags properly also don't echo like that
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<div id = "bookbar">
<img src= "<?php echo $row['image'] ?>" alt = "image">
<p><?php echo $row['name']; ?></p>
</div>
<?php
}
}
$conn->close();
?>
If you want to echo something out
Its better to close on open the php tags like so
PHP goes here
?>
HTML goes here
<?php
PHP goes here
And if you want to echo something inside the HTML just do this
<span> <?php echo "something" ?> </span>
much easier and makes the code easier to read.
change your echo statement to -
echo '<div id = "bookbar"><img src= "' . $row['image'] . '" alt = "image"><p>'. $row['name'] .'</p>'
Your issue is a syntax problem - you can't use echo like that, it has to echo a string variable. You should be seeing an error message about it.
You could keep the echo statement and put all the HTML inside a string, and concatenate (or interpolate) the PHP data into it. But IMO the easiest thing here in terms of readability and maintenance is to step out of the PHP tags, print the HTML, embed some PHP tags in it for the variables, and then step back in again to continue with the code. It makes the HTML far easier to understand:
?>
<div id="bookbar">
<img src="<?php echo $row['image'] ?>" alt="image">
<p><?php echo $row['name'] ?></p>
</div>
<?php
When you are in php mode you should echo strings as php variables wrapped with single quotes:
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo '<div id = "bookbar">';
echo '<img src="' . $row['image'] . '" alt = "image">';
echo '<p>' . $row['name'] . '</p>';
echo '</div>';
}

If statement to not show div / h2

This for each / if statement displays changes, and then right below it it displays the changes made.
I am trying to write an if statement that tells it not to show the h2 and the #change_box if there are no changes.
Help would be greatly appreciated.
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<? foreach ($audit['Events'] as $event):?>
<?if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?= $event['Field']?>
</span>:
<?= $event['Value'] ?>
<?=($event['Previous'])?>
<?endif?>
<?endforeach?>
</div>
<?php
if ($changes) { // You'll have to set this variable
//Echo all of your HTML
else {
//Echo the stuff you'd rather show if they didn't change anything
}
?>
To give you an idea of how I'd write your code
<?php
if ($changes) {
echo '<h2 class="changes">Changes:</h2>';
echo '<div id="change_box">';
foreach ($audit['Events'] as $event) {
if ($event['Type'] != 'Comment') {
echo $event['Field'] . </span> . $event['Value'] . $event['Previous'];
}
}
echo "</div>"
}
?>
You could wrap your code with an if like
if(count($audit['Events'])>0)
{
//Your code
}
Wny do you open and close php tags every time? Would be better to make everything in PHP and echo whenever you want to print HTML code.
<?php
echo "<h2 class=\"changes\"> Changes: </h2>";
echo "<div id=\"change_box\">";
foreach ($audit['Events'] as $event) {
if ( $event['Type'] != 'Comment') {
echo "<span class=\"field\">".$event['Field']."</span>";
echo $event['Value'];
echo "(".$event['Previous'] .")";
}
}
echo "</div>";
?>
please make space after each <? and make sure you enabled short tags
<?php if(is_array($audit['Events']) && $audit['Events']):?>
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<?php foreach ($audit['Events'] as $event):?>
<?php if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?php echo $event['Field'];?>
</span>:
<?php echo $event['Value']; ?>
<?php echo $event['Previous'];?>
<?php endif;?>
<?php endforeach;endif;?>
</div>

Dynamically populating an UnOrdered List

I am attempting following code to populate an UnOrdered List dynamically. The same type of code I am successfully using to populate a DropDown. But when I changed the tags to UnOrdered List, it is not working. When run, it just displays some tags instead of the actual output.
Where is the error:
<?php
require("dbconnection.php");
require("dbaccess.php");
$divName = $_GET['DivName'];
$ulName = $_GET['ControlName'];
$query = $_GET['SqlQuery'];
echo $query;exit;
dbconnection::OpenConnection();
$result = dbaccess::GetRows($query);
?>
<ul id="<?php echo $ulName; ?>" name="<?php echo $ulName; ?>">
<?php while($row=mysql_fetch_array($result))
{ ?>
<li><?php echo $row[1]; ?>"></li>
<?php } ?>
</ul>
The code that I used to populate a DropDown is below: It works absolutely fine:
<?php
require("dbconnection.php");
require("dbaccess.php");
$dropdownControlName = $_GET['DropDownControlName'];
$query = $_GET['SqlQuery'];
dbconnection::OpenConnection();
$result = dbaccess::GetRows($query);
?>
<select id="<?php echo $dropdownControlName; ?>" name="<?php echo $dropdownControlName; ?>">
<option>Select from the list</option>
<?php while($row=mysql_fetch_array($result))
{ ?>
<option value="<?php echo $row[0]; ?>"><?php echo $row[1]; ?></option>
<?php } ?>
</select>
The error is here:
<li><?php echo $row[1]; ?>"></li>
should be this:
<li><?php echo $row[1]; ?></li>
Don't know Php but what this line doing :
echo $query;exit;
Are you sure that you have to use the second field of the result set?
<li><?php echo $row[1]; ?>"></li>
There is an extra >" there.
Can you show us the resulting html code ?
SORRY: I was away from the PC for some minutes before posting, just saw that I answered the same as the following answer.
HI,
I used your code and gave some constant values and i got the following out put.
Out Put:
1">
2">
3">
4">
Used code:
`
">
`
and final Conclusion is, this prints the unordered list and i think yo may check your
echo $row[1]; part for any html out puts.
Note: "> This tag comes because of in your code having this value

Categories