PHP Manipulating MySQL Results? [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I need some little help on a simple thing!
I have this table in my database, where I have a number of images stored, called "covers".
And then I have a table in HTML that displays the covers of this MySQL table inside one of it's cells.
Like this:
<td><a href="movie.php?id=<?php echo $idt + 1; ?>">
<?php echo '<img src="'.htmlentities($idt + 1['cover'], ENT_QUOTES, 'UTF-8').'" alt="Cover" style="max-width:300px;max-height:300px;" />';; ?></a></td>
And this is the code before
$req = mysql_query("select id, name, year, genre, cover from movies");
$dnn = mysql_fetch_array($req);
$idt = $dnn['id'];
But why doesn't it work when I try to dynamically change id by putting this?
$idt + 1;

In order to output data from multiple rows, I suggest using a loop like this:
while ($row = mysql_fetch_assoc($req)) {
?><td>
<a href="movie.php?id=<?php echo $row['id']; ?>">
<img src="<?php echo htmlentities($row['cover'], ENT_QUOTES, 'UTF-8'); ?>" alt="Cover" />
</a>
</td><?php
}

You need to fetch a whole set of results and not just one id and increment it in the view. You should do a foreach loop on $idt like that:
foreach($dnn as $row){
$id=$row['id'];
$cover=$row['cover'];
$genre=$row['genre'];
//etc...
//now echo html with vars like this:
echo "<img src=\"$cover\"/>";
}

Related

PHP echo results into a table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am struggling to put the data that is pulled from my database into a table layout.
This is the echo result:
echo "<p><h3>".$results['ID Number']."</h3>".$results['id_number']."</p>";
echo "<p><h3>".$results['Card Status']."</h3>".$results['card_status']."</p>";
echo "<p><h3>".$results['Full Name']."</h3>".$results['full_name']."</p>";
echo "<p><h3>".$results['DBS/CRB Number']."</h3>".$results['dbs_number']."</p>";
echo "<p><h3>".$results['Job Title']."</h3>".$results['job_title']."</p>";
echo "<p><h3>".$results['Card Start Date']."</h3>".$results['card_start_date']."</p>";
echo "<p><h3>".$results['Card Expiry']."</h3>".$results['card_expiry_date']."</p>";
echo "<img src='photos/".$results['photo_name']."'>";
Please can someone help me, its driving me crazy, also the Name of the result isn't showing, e.g. 'ID Number' as a title then 'id_number' result.
or is there an easier way to show all the data from the record rather than having multiple echo's
I have a search box to find a record matching the id_number and then the data connected to a id_number is then shown
Well I dont think there was any help needed , still for the sake of your blockade use this as
echo "<table><tr><td><h3>ID Number</h3></td><td>".$results['id_number']."</td></tr>";
echo "<tr><td><h3>Card Status</h3></td><td>".$results['card_status']."</td></tr>";
echo "<tr><td><h3>Full Name</h3></td><td>".$results['full_name']."</td></tr>";
echo "<tr><td><h3>DBS/CRB Number</h3></td><td>".$results['dbs_number']."</td></tr>";
echo "<tr><td><h3>Job Title</h3></td><td>".$results['job_title']."</td></tr>";
echo "<tr><td><h3>Card Start Date</h3></td><td>".$results['card_start_date']."</td></tr>";
echo "<tr><td><h3>Card Expiry</h3></td><td>".$results['card_expiry_date']."</td></tr>";
echo "<tr><td></td><td><img src='photos/".$results['photo_name']."'></td></tr></table>";
I'd personally store those into an array and loop over it.
Something like this:
<?php
$array = array();
$array[] = $results['ID Number'] . ' ' . $results['id_number'];
//And so on until all the values you want are in the array.
//You could potentially use a loop for this too, depending on your code
echo '<table>';
foreach($array as $value){
echo '<tr>';
echo '<td> $value </td>';
echo '</tr>;
}
echo '</table>';
As for the name of the result not showing up. It's hard to pin point that out without looking at the rest of the code.

Use echo in a css class [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am using php echo throughout different pages on a project, to update a percentage value that is changing daily. This value can be negative or positive.
Here is my code:
<i class="icon-thumbs-up"><strong> <?php $file = file('include.txt');echo $file[n]; ?></strong></i>
I am using FontAwesome icons with a Bootstrap template. Everything is working fine like this.
Now, if the percentage is negative, I would like to use class="icon-thumbs-down" instead of class="icon-thumbs-up".
I have tried to achieve this using
<i class="<?php $file = file('include.txt');echo $file[n]; ?>"><strong> <?php $file = file('include.txt');echo $file[13]; ?></strong></i>
in order to change it on all pages.
However, this is not working. Thanks for any hints!
To clarify:
<i class="icon-thumbs-up"><strong> <?php $file = file('include.txt');echo $file[0]; ?></strong></i>
Content of include.text: 0.58% on line 1 -> All working fine. I got the thumbs up displayed and next to it the value 0.58%.
Now I tried to change to:
<i class="<?php $file = file('include.txt');echo $file[1]; ?>"><strong> <?php $file = file('include.txt');echo $file[0]; ?></strong></i>
Content of include.text: 0.58% on line 1 and icon-thumbs-up on line 2. (Which I would like to change in the include.txt on a daily basis to icon-thumbs-up or icon-thumbs-down, depending on the value of line 1.)
If value from file('include.txt') is integer then you can use ternary operator to echo correct css class, eg. like this:
<li class="<?php echo (int) $file[0] < 0 ? 'icon-thumbs-down' : 'icon-thumbs-up'; ?>"></li>
This is a bit hard to reply to as your code isnt that clear. I can however hand you the following suggestion via an example:
$someInt = 10;
echo $someInt < 0 ? 'icon-negative' : 'icon-positive'; // will echo positive
$someInt = -3;
echo $someInt < 0 ? 'icon-negative' : 'icon-positive'; // will echo negative
This is a short if/else (ternary). To demonstrate how it works, this would be the same in a full syntax:
$someInt = -3;
if($someInt < 0){
echo 'icon-negative';
}
else{
echo 'icon-positive';
}
I would do the following:
<?php
// your logic here, move in another file if this gets bigger:
function percentage($nb) {
$file = file('include.txt');
return $file[$nb];
}
?>
<i class="<?php echo percentage(5) > 0 ? 'positive' : 'negative' ?>">
<strong>Text</strong>
</i>

echo inside an echo - does it work? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
It's been a long time since I have done php so sorry for the silly question.
This is my current code, I'm trying to save the URL as a variable so that I can insert it into the echo, but it doesn't seem to work as nothing appears:
<?php ob_start();
echo get_post_meta($post->ID, 'oldurl', true);
$old_url = ob_get_contents();
ob_end_clean();
?>
<?php echo do_shortcode('[fbcomments][fbcomments url="$old_url" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
I have echoed $old_url and can see that it has the correct value, but how do I insert the value into the echo do_shortcode with url="$old_url"?
This doesn't work either:
<?php echo do_shortcode('[fbcomments][fbcomments url="echo $old_url;" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
You'll need to switch your quotes around. Single quotes print everything out as-is. Double-quotes will process the variables. Also, echo is not needed within an echo.
<?php echo do_shortcode("[fbcomments][fbcomments url='$old_url' width='375' count='off' num='3' countmsg='wonderful comments!']"); ?>
Another way to do it without switching your quotes is to break out of the statement:
<?php echo do_shortcode('[fbcomments][fbcomments url="'.$old_url.'" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
Variables are not replaced in single quotes ...
<?php echo do_shortcode('[fbcomments][fbcomments url="' . $old_url . '" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
Singles quotes doesn't allow variables parsing.
For example :
$var = 'Hello';
echo 'The content of my var is : $var';
// Will output : "The content of my var is : $var"
echo "The content of my var is : $var";
// Will output : "The content of my var is : Hello"
So you have to use double quotes or use the concatenate operator : .

how to search using a textfield? php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I want to search a whole news feed by inserting a word in my text field. I have no idea how to do this. Here is my code, so you know what I mean with it.
<form action="search.php" method="get">
<tr><th>search: </th><td><input type="text" name="search" value="{$word}"></td></tr>
</form>
How can I check if the word I inserted in the searchbar, exists somewhere in the news feed?
My news feed is used like this:
$xml=simplexml_load_file("newsfeed.xml");
foreach($xml->channel->item as $item) {
echo '<h1 class="title">' . $item->title . '</h1>';
echo '<p class="desc">'.$item->description."</p>";
}
I think you can use the function strpos.
Find the numeric position of the first occurrence of needle in the
haystack string.
Example:
$a = 'Long text to look into it.'
if (strpos($a, 'it') !== false)
echo 'true';
In your case, you can use strpos either to find the word in the item title or the item description:
$a = $_GET['search'];
foreach($xml->channel->item as $item) {
print_result = 0; // flag to know if the search is in the feed.
if (strpos($item->description, $a) !== false){
print_result = 1;
} // end if desc
if (strpos($item->title, $a) !== false){
print_result = 1;
} // end if title
if(print_result == 1){
echo '<h1 class="title">' . $item->title . '</h1>';
echo '<p class="desc">'.$item->description."</p>";
} //end if print results.
} // end foreach
I would make a foreach like you do it now:
And make kind of relevance points for each news item. Something like if it's in the title then it's more relevant and if it's at the beginning and so on.
Then you can write it into an array and sort it.
I hope you have something like an id in your newsfeed.xml for this.

Load HTML <select> options while loading the page in php/javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to load option dynamically,when we have to load options like eg: 1-500. as the values in select list.
Loding can be either done in php or javascript.
plz can someone provide eg/simple snippet or helpfull links? Thanks!
I tried: Javascript
for(var i=1;i<=500;i++)
{
//create new option
var option = new Option(i, "Value" + i);
//Add the new option it to the select
sel.options[i] = option;
}
Iterate over the values with a foreach, for or while loop, and just ouput whatever you need:
<?php
$html = '<select>';
for ($i=0; $i<500; $i++) {
$html .= '<option value="' . $i . '">Option ' . $i . '</option>';
}
$html .= '</select>';
echo $html;
?>

Categories