i don't know a lot of php but, i have the following code, it will load all images from the top of the table to the bottom, can i invert this process? load from de bottom to top? I need the ordest lines to be loaded first...
<?php
mysql_connect("localhost","root","");
mysql_select_db("bravo");
$res=mysql_query("select * from coisas");
?>
<div>
<?php
while ($row=mysql_fetch_array($res)) {
echo "<img src=\"{$row['imagem']}\">";
}
?>
</div>
If your table have one identifier column you can do that (assuming id is the identifier column name):
<?php
mysql_connect("localhost","root","");
mysql_select_db("bravo");
$res=mysql_query("SELECT * FROM coisas ORDER BY id DESC");
?>
<div>
<?php
while ($row=mysql_fetch_array($res)) {
echo "<img src=\"{$row['imagem']}\">";
}
?>
</div>
As Mark Baker correctly say, let SQL do it for you with a ORDER BY.
If you still want to do this in PHP, use this snippet of code:
<?php
$html = '';
while ($row=mysql_fetch_array($res)) {
html = "<img src=\"{$row['imagem']}\">".$html;
}
echo $html;
?>
This will concatenate your current (of the loop) fetched array entry with previous one. So, the first will be concatenated before the last (read it as inversed order).
Moreover, don't use msql_* as those functions are deprecated. Use msqli_* or PDO instead
PS.: This code is your code revisited, if there is any error (in fetching array, connection or so) please correct it your own: I only give you a pointer
Related
I'm still a php novice...I apologize if this is a silly question.
I have an array of numerical values ('views'), each 'view' specific to a video. I want to output the top 'views' into cells of a table (html). Whether my list is ascending or descending, I continue to only see the first number (but the correct number) of the list ($views1 in the first row). Here is my code:
<? php //lots of code, followed by
include 'connect.php';
$views=mysql_fetch_array(mysql_query("SELECT video.views FROM video ORDER by
video.views DESC LIMIT 10"));
list($views1,$views2,$views3,$views4,$views5,$views6,$views7,$views8,$views9,$views10)
= $views;
?>
<html>
<body>
<table>
<tr><td><?php echo $views1 ?></td></tr>
<tr><td><?php echo $views2 ?></td></tr>
. //My actual code has 10 rows going from $views1 to $views10
.
.
<tr><td><?php echo $views10 ?></td></tr>
</body>
</html>
I just cannot see what the problem is...thanks for any help!
This code should work:
<?php //lots of code, followed by
include 'connect.php';
$q=mysql_query("SELECT video.views FROM video ORDER by video.views DESC LIMIT 10");
$views=array();
while ($views[]=mysql_fetch_array($q));
?>
<html>
<body>
<table>
<?php
foreach ($views as $view)
echo '<tr><td>'.$view['views'].'</td></tr>';
?>
</table>
</body>
</html>
Don't forget that you shouldn't write duplicate codes, loops are there for you.
use
var_dump($views);
before your list(...) statement to debug it. It may be $views isn't being populated like you think.
That's because mysql_fetch_array fetches one row, so even assigning a list of variables in this manner will only set as much as there is colunms in the result - one, in your case. Use something like that instead:
$res = mysql_query(...);
while ($arr = mysql_fetch_array($res)) $views[] = $arr[0];
<tr><td><?=$views[0]?></td></tr>
...
By the way, mysql extension is deprecated, use mysqli instead.
Basically I have a query called using PHP:
<?php $result = mssql_query("SELECT * FROM Colours WHERE Type = 'type1' ");
while ($row = mssql_fetch_array($result)) {
if ($row['ColourID'] == "1") {
$sayclass1="imgactive";
}else{
$sayclass1="imginactive"; }
?>
As you can see once I execute the query I then loop it, the problem is that it returns an array, now in some instances I need to use the full array, but I would like to be able to select one entry from it for if statements and such. For example:
<img id="h" src="<?php echo $row['thumbimg']; ?>
now thumbimg is a column in my DB, and it just holds a url. However due to the fact its an array the picture doesn't display because its echoing all the values, so instead of images/image1.png for example it is echoing images/image1.png images/image2.png images/image3.png etc etc...
I hope that makes sense, and can anyone tell me how to manipulate the query/code slight to still return all the entries but to select certain values from the arrays please?
You need to use the img tag with in for each if you want to show all the images
foreach($row[thumbimg] as $img):
<img id="h" src="<?php echo $img; ?>
end foreach;
<?php $result = mssql_query("SELECT * FROM Colours WHERE Type = 'type1' ");
while ($row = mssql_fetch_array($result)) {
if ($row['ColourID'] == "1") {
$sayclass1="imgactive";
}else{
$sayclass1="imginactive";
}
echo '<img id="h" src="'.$row['thumbimg'].'">';
}
?>
Using this above code, you will get one image per line from the database. The part of code you have copy-pasted is not enough to determine where your mistake is and why $row['thumbimg'] contains a concatenated value of the result.
not sure how feasable this is, but I have just rolled my own user search form, which simply queries my database and returns all the results with any given username, or similar using the LIKE 'some_username%' statement.
My search works great, and im really chuffed with myself as I am a php and mysql novice.
I used a mysql_fetch_assoc($result) statement, and then used a while loop to echo out each row from the database into an html table.
What I would then like to be able to do, is select a record from the table, and open a new page, which is populated with all the fields for that record, which I can then use to edit and update the user settings.
I thought perhaps one way to do it, is to perhaps echo out a form instead? that way I can have a button next to each row, to post the fields into some php code on my new page? I thought this may be a bit clunky though, and not sure how I would go about echoeing out a different form for each row.
Don;t know if anyone had any ideas on the best way to do this? If you need any code examples of what im working with, I can post them here.
Thanks very much!!
Eds
not a form but a hyperlink.
I wonder why you aren't familiar with this way of opening new pages as it is used everywhere.
just create a hyperlink
name
here is a sketch example of such an application, editing only one field, but you can add any number as well:
a main script:
<?
mysql_connect();
mysql_select_db("new");
$table = "test";
if($_SERVER['REQUEST_METHOD']=='POST') { //form handler part:
$name = mysql_real_escape_string($_POST['name']);
if ($id = intval($_POST['id'])) {
$query="UPDATE $table SET name='$name' WHERE id=$id";
} else {
$query="INSERT INTO $table SET name='$name'";
}
mysql_query($query) or trigger_error(mysql_error()." in ".$query);
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
exit;
}
if (!isset($_GET['id'])) { //listing part:
$LIST=array();
$query="SELECT * FROM $table";
$res=mysql_query($query);
while($row=mysql_fetch_assoc($res)) $LIST[]=$row;
include 'list.php';
} else { // form displaying part:
if ($id=intval($_GET['id'])) {
$query="SELECT * FROM $table WHERE id=$id";
$res=mysql_query($query);
$row=mysql_fetch_assoc($res);
foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v);
} else {
$row['name']='';
$row['id']=0;
}
include 'form.php';
}
?>
and two simple templates responsible for output,
one for the displaying the form, form.php
<? include TPL_TOP ?>
<form method="POST">
<input type="text" name="name" value="<?=$row['name']?>"><br>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="submit"><br>
Return to the list
</form>
<? include TPL_BOTTOM ?>
and one to display the list, list.php
<? include TPL_TOP ?>
Add item
<? foreach ($LIST as $row): ?>
<li><?=$row['name']?>
<? endforeach ?>
<? include TPL_BOTTOM ?>
always start php like this <?php, usually php manual configuration do not support short tag like this <?.
for hyperlink just use view record
It is just query string and get id on next page like this
$id = $_GET['id'];
Hope u will understand..
I am trying to position the data pulled from my mysql database using the following php code on my webpage;
<?php
require "connect.php";
$query = "select * from item LIMIT 0, 1";
$result = #mysql_query($query, $connection)
or die ("Unable to perform query<br>$query");
?>
<?php
while($row= mysql_fetch_array($result))
{
?
//call each item from the database, and nest in div or html table
<?=$row['item']?>
<?=$row['description']?>
<?=$row['brand']?>
When I view the webpage it creates the row for each item however each field is empty?
The conection to the database is fine as I ran another piece of code which simply displayed all the info in 1 block. However this is not what I want I want to be able to position each item where I want it on the site.
I wrote this when I had php version 4.1 running on IIS I am now running the latest version of php 5 and after doing some reading it says there are chnages in the syntax including global variables disabled by default so not sure if this is the issue?
try:
<?php echo $row['item'] ?>
<?php echo $row['description'] ?>
<?php echo $row['brand'] ?>
I believe the <?= ?> syntax is normally disabled by default.
The following will also show a dump of you $row array, so you can make sure that it actually contains data
<?php print_r($row); ?>
Good luck!
Sorttag maybe no enabled, try :
<?php echo $row['item']; ?>
<?php echo $row['description']; ?>
<?php echo $row['brand']; ?>
I have two tables and I want to show a list items of particular table as hyperlink only if its value exist in another table. Otherwise it show value as plain text not hyperlink.
Considering example...
$result= mysql_query("select * from tbl_songlist_info order by song_title") or die(mysql_error());
$resultpoet= mysql_query("select * from tbl_poet_info") or die(mysql_error());
$rowpoet= mysql_fetch_array($resultpoet);
while($row = mysql_fetch_array($result))
Now I have to show values of $row as hyperlink only if its value exist in $rowpoet.. I have used in array function. please chk it out...
<? if (in_array($row['poet_name'],$rowpoet)) { ?>
<a href="poet.php?title=<?=$row['poet_name'] ?>"><? } ?>
<?=$row['poet_name'] ?> </a>
please check this code. All value are showing as plain text if value exist in other table than also...
I think you want something like :
<? if (in_array($row['poet_name'],$rowpoet)) { ?>
<?=$row['poet_name'] ?>
<? } else { ?>
<?=$row['poet_name'] ?>
<? } ?>
unless i misunderstand that will check for 'poet_name' field inside $rowpoet array and if it exists put a link, otherwise if not in array then put plain text.
Hope that helps