Fixing my search form in HTML with embedded PHP? - 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);
?>

Related

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>

For each inside other for each on view MVC PHP

I'm trying to give structure to my code and i am facing a problem.
I'm looping through a sql query response and for each element i'm trying to retrieve other related elements. It works in my controller without problem but when i'm trying to repeat in the view I always get the same value for the related element
My controller:
<?php
include_once('class/guide.class.php');
$bdd = new DBHandler();
$req = guide::getGuides($bdd,0,5);
foreach ($req as $results => $poi)
{
$req[$results]['id'] = htmlspecialchars($poi['id']);;
$req[$results]['name'] = nl2br(htmlspecialchars($poi['name']));
$guide = new guide($results['name'],$bdd);
$guidePois = $guide->getGuidePois($poi['id']);
foreach ($guidePois as $res => $re)
{
echo $guidePois[$res]['id'];
echo $guidePois[$res]['name'];
$guidePois[$res]['id'] = htmlspecialchars($re['id']);
$guidePois[$res]['name'] = nl2br(htmlspecialchars($re['name']));
}
}
include_once('listing.php');
here, you see that I echo the ids/names of the related list of element and it works well, the output is correct for each element of the first list.
When i do it in my view:
<?php
foreach($req as $poi)
{
?>
<div class="news">
<h3>
<?php echo $poi['id']; ?>
<em>: <?php echo $poi['name']; ?></em>
</h3>
<?php foreach($guidePois as $re)
{
?>
<h4>
<?php echo $re['id']; ?>:
<?php echo $re['name']; ?>
</h4>
<?php
}
?>
</div>
<?php
}
?>
Somehow the first list output are the good elements, but for the 2nd list, i always get the related elements of the first item.
Do you have an idea ?
Thanks a lot for your help
This is because you only set:
$guidePois = $guide->getGuidePois($poi['id']);
once in the controller.
If you want it to work in the view, you need to insert this code right after the closing </h3>
<?php $guidePois = $guide->getGuidePois($poi['id']); ?>
So that $guidePois gets a new value in each iteration.
Complete view code:
<?php
foreach($req as $poi)
{
?>
<div class="news">
<h3>
<?php echo $poi['id']; ?>
<em>: <?php echo $poi['name']; ?></em>
</h3>
<?php
$guidePois = $guide->getGuidePois($poi['id']);
foreach($guidePois as $re)
{
?>
<h4>
<?php echo $re['id']; ?>:
<?php echo $re['name']; ?>
</h4>
<?php
}
?>
</div>
<?php
}
?>

How to echo a list into a textarea?

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
}
?>

PHP Error: Trying to get property of non-object

I am working in CodeIgniter framework and I have tried to apply all the other solutions I found on stack but I could not make it work so here is my problem...
I am trying to retrieve a record from MySQL database table called 'questions' based on uri segment. Then I am trying to display this record in a view. As far as I can tell, the uri segment is passed along everywhere it needs to be, and record is retrieved from database.
The problem comes up when I am trying to access the data from the controller, in my view.
Error I am getting for each echo in my loop in 'view_thread_view' is
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/view_thread_view.php
Any solutions would be greatly appreciated.
Here is my code:
Controller thread
function view_thread()
{
$quest = $this->uri->segment(3);
echo $quest;// for checking if uri segment is passed
if($query = $this->data_model->get_thread($quest))
{
$data['records'] = $query;
}
$this->load->view('view_thread_view', $data);
Model data_model
public function get_thread($quest)
{
if($q = $this->db->get_where('questions' , 'qid' , $quest));
{
return $q;
}
}
View view_thread_view
<div title="question_view">
<h1>Question View<h1>
<?php foreach($records as $data) : ?>
<div>
<h2>Title</h2>
<?php
echo $data->title;
?>
</div>
<div>
<h2>Question</h2>
<?php
echo $data->contents
?>
</div>
<div>
<h2>Tags</h2>
<?php
echo $data->tags
?>
</div>
<div>
Thread owner
<?php
echo $records->uname
?>
</div>
<?php endforeach; ?>
</div>
EDIT: QUESTION ANSWERED
Thanks to Girish Jangid fixed the problem this is the working code:
Controller
function view_thread()
{
$quest = $this->uri->segment(3);
echo $quest;// for checking if uri segment is passed
//$data = array();
if($query = $this->data_model->get_thread($quest))
{
$data['records'] = $query;
}
$this->load->view('view_thread_view', $data);
}
Model
public function get_thread($quest)
{
if($q = $this->db->get_where('questions' , array('qid' => $quest)));
{
return $q;
}
}
View
<div title="question_view">
<h1>Question View<h1>
<?php foreach($records->result() as $data) : ?>
<div>
<h2>Title</h2>
<?php
echo $data->title;
?>
</div>
<div>
<h2>Question</h2>
<?php
echo $data->contents
?>
</div>
<div>
<h2>Tags</h2>
<?php
echo $data->tags
?>
</div>
<div>
Thread owner
<?php
echo $data->uname
?>
</div>
<?php endforeach; ?>
</div>
</div>
You should user db function to fetch results, please use CodeIgniter db Query Results functions, like this
if($records->num_rows() > 0){
foreach($records->result() as $data){
if(isset($data->title)) echo $data->title;
}
}
For more detail please read CodeIgniter Query Result function
Query Results
Try this code
<div title="question_view">
<h1>Question View<h1>
<?php if($records->num_rows() > 0) { ?>
<?php foreach($records->result() as $data) : ?>
<div>
<h2>Title</h2>
<?php
echo $data->title;
?>
</div>
<div>
<h2>Question</h2>
<?php
echo $data->contents
?>
</div>
<div>
<h2>Tags</h2>
<?php
echo $data->tags
?>
</div>
<div>
Thread owner
<?php
echo $records->uname
?>
</div>
<?php endforeach; ?>
<?php } ?>
</div>
$records is an array, not an object, therefore you cannot do $records->uname. You probably meant $data there too.
Edit: On second look, it appears $data is an array as well! Arrays are accessed via ['key'] not ->key
Also, your code is way over complicated.
<?php
foreach($records as $data){
$string = <<<STR
<div>
<h2>Title</h2>
{$data['title']}
</div>
...etc
STR;
echo $string;
}
http://www.php.net/manual/en/language.types.string.php

Passing an array of variables FROM an object

So here's what I want to do, I have a class and within SomeClass, there's a function consists of 2 variables, each has a numerical value. I want to return an array of the two variables which have dependencies in post data outside of SomeClass from a form that posts to itself.
<?php
class SomeClass{
private $someVar1;
private $someVar2;
public function setVars($var1,$var2) {
$this->someVar1 = $var1;
$this->someVar2 = $var2;
}
function __construct() {
}
function setFraction() {
$sum['num'] = 140*pow($this->someVar1,0.75);
$sum['den'] = $sum['num']/$this->someVar2;
return $sum;
}
}
if(isset($_POST['num'])){$num = preg_replace('/\D/', '', $_POST['num']);}
elseif(isset($_POST['den'])){$den = preg_replace('/\D/', '', $_POST['den']);}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Numerator:<input type="text" value="<?php if(isset($num)){echo $num;}?>" name="num" /><br />
Denominator:<input type="text" value="<?php if(isset($den)){echo $den;}?>" name="den" /><br />
<input type="submit" value="Go" name="Go">
</form>
<?php if(isset($num,$den)):
$obj = new SomeClass();
$obj->setVars($num,$den);
$sum=$obj->setFraction();?>
<pre>
<?php print_r($obj->setFraction());?>
</pre>
<ul>
<?php foreach($sum as $val):?>
<li><?php echo $val['num']?></li>
<li><?php echo $val['den']?></li>
<?php endforeach;?>
</ul>
<?php endif;?>
When I run the script, post data for $num and $den is being sent and returned with setFraction(), however when I try to iterate through the array in a foreach loop, I don't get any return data. I'm sure its something simple, I've tried it with a foreach loop and without with same results, any help would be excellent, thx.
EDIT:
<?php if(isset($num,$den)):
$obj = new SomeClass();
$obj->setVars($num,$den);
$array = $obj->setFraction();?>
<ul>
<li><?php echo $array['num']?></li>
<li><?php echo $array['den']?></li>
</ul>
<?php endif;?>
Store the returned data to some variable,and then iterate.
//
<?php if(isset($num,$den)):
$obj = new SomeClass();
$obj->setVars($num,$den);
$array = $obj->setFraction();?>
<ul>
<?php foreach($array as $val):?>
<li><?php echo $val['num']?></li>
<li><?php echo $val['den']?></li>
<?php endforeach;?>
</ul>
<?php endif;?>
you haven't assign $sum=$obj->setFraction(); to $sum
auYou're not assigning the return values of $obj->setFraction() to a variable. You need to do:
$sum=$obj->setFraction();
and all should be OK.
Just becuase you're returning a variable called $sum doesn't mean that that variable name is used once it's back in the main body of your script.
I have just noticed, as you rightly state in your acceptance, that you don't need the foreach block. Just use $sum['num'] and $sum['den']. Sorry about that - I should have noticed earlier
You should use $obj->sum instead of $sum.

Categories