Welcome to WebmasterWorld Guest from 54.158.228.55

Forum Moderators: open

Message Too Old, No Replies

Calling rows based on keyword

     
1:24 am on Aug 14, 2010 (gmt 0)

5+ Year Member



I would like to print part of a row. The row is selected by a search with a keyword.

table name: items

item_no name totA totB totC totD totE
01001 house 10 20 30 40 50
01002 food 15 25 35 45 55
01003 work 115 125 135 145 155


I want to print only the columns 'name', 'totA', 'totB', 'totC' and 'totE'

So far, I use this as the search form
<form action="/vitamine/wp-content/themes/twentyten/insert.php" method="post">
Value1: <input type="text" name="value1"><br>
<input type="Submit">
</form>


And this to call the data
<?php
$username="root";
$password="";
$database="test";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM items WHERE name LIKE '%".mysql_real_escape_string($_POST['value1'])."%'";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<table border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Value1</font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"Shrt_Desc");

?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>

</tr>

<?php
$i++;
}

?>
</table>


But this gives me a list of all search results items and no rows. How can I print the whole row minus the columns I don't want?

Thanks again!
3:23 am on Aug 14, 2010 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



By selecting the columns you want, instead of * which selects all columns. also the "counter method" (I call it) is not needed to step through recordsets unless you specifically need a count of rows. Like so . . .

$results = null; // squelches concatenation errors and makes for a nice if at the end
$query="SELECT name, totA, totB, totC, totD, totE FROM items WHERE name LIKE '%".mysql_real_escape_string($_POST['value1'])."%'";

$result=mysql_query($query) or die("could not execute query: " . mysql_error());
while ($row=mysql_fetch_array($result)) {
$results .= '<tr>
<td>' . $row['name'] . '</td>
<td>' . $row['totA'] . '</td>
<td>' . $row['totB'] . '</td>
<td>' . $row['totC'] . '</td>
<td>' . $row['totE'] . '</td>
</tr>';
}
mysql_close();
//
if ($results) {
echo "<table border=\"1\" align=\"center\" cellpadding=\"2\" cellspacing=\"2\">
<tr><td>NAME</td><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td></tr>
$results
</table>
";
}
else { echo "<p>No results were found with that search.</p>"; }
?>


May contain typos, but that should give you an idea.
5:42 am on Aug 14, 2010 (gmt 0)

5+ Year Member



Looks great (and simple)

I got the idea and I want to try it out now, but my wife won't let me :)

Thanks!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month