Trouble displaying values...

CannonBallGuyCannonBallGuy ModeratorShared Hoster
Ok, I got my search script working exactly how I like now.
When results are displayed, an input box and a button are displayed next to each result.

Here is the search script:
[PHP]<html>
<head><title>... test search ...</title></head>
<body>

<form name="form" action="srch.php" method="get">
<input type="text" name="keywords" />
<input type="submit" name="Submit" value="Search" />
</form>

<?php

if(isset($_GET))
{
$keywords = $_GET;
}

$limit=25;

if ($keywords == "")
{
echo "<span style=\"color:#FF0000;\">Please enter a search...</span>";
exit;
}

// Open Connection To mySQL.
// Replace 'username' and 'password' with appropiate login values for your server.

$mysqlLink = mysql_connect( 'localhost' , 'matsim68_test' , 'test' );

// Select mySQL Database.
// Replace 'database' with appropiate database for your server.

mysql_select_db( 'matsim68_test' , $mysqlLink ) or die("Can't c0nn3ct 2 da d8abase!?!");

// Select mySQL Table.
// Replace '$table' with appropiate table for your server.

$table1 = 'tab';
$col1 = 'f1';
$col2 = 'f2';
$col3 = 'f3';

$query = "SELECT * FROM `".$table1."` WHERE `".$col1."` LIKE '".$keywords."' OR `".$col2."` LIKE '".$keywords."' OR `".$col3. "` LIKE '".$keywords."' ORDER BY `".$col1."` DESC";

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults) or die(mysql_error());

// If we have no results, offer a google search as an alternative

if ($numrows == 0) {
echo "Results";
echo "Sorry, your search: " . $trimmed . " returned zero results";
}
// !! * LINE 50 * !! //
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}

// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "You searched for: " . $keywords . " <br />";

// begin to show results set
echo "Here are your results: <br />";
echo "<form name=\"buyform\" action=\"buy.php\" method=\"get\">";
$count = 1 + $s ;

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title1 = $row[$col1];
$title2 = $row[$col2];
$title3 = $row[$col3];

$pro = "product".$count;
$qty = "quantity"."$count";

echo "$count) $title1, $title2, $title3, <a href=\"file:///Users/Matthew/PIPS/$title1/$title2/$title3.txt\" target=\"_blank\">Click Here...</a> <input type=\"text\" name=\"$qty\" size=\"4\" value=\"100\"> <input type=\"text\" name=\"$pro\" value=\"$title3\"> <input type=\"submit\" value=\"BUY THIS\"><br />";
$count++ ;
}

echo "count is now $count - <input type=\"text\" name=\"count\" size=\"3\" value=\"$count\"><br /></form>";

$currPage = (($s/$limit) + 1);

//break before paging
echo "<br />";

// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "Prev 10 Link";
}

// calculate number of pages needing links
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}

// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
$news=$s+$limit;
echo "Next 10 Link";
}

$a = ($s + $limit);
if ($a > $numrows) {
$a = $numrows;
}
$b = $s + 1 ;
echo "<p>Showing results...</p>";

?>
</body>
</html>[/PHP]

You can see it sends data (via GET) to buy.php like: 'product1', 'quantity1', 'product2', 'quantity2', 'product3', 'quantity3', etc.

So, clicking a button on the search-results page would give an url like this:
buy.php?quantity1=100&product1=Mrk4&quantity2=100&product2=Mrk5&quantity3=100&product3=Fire&quantity4=100&product4=Poo&count=5

Here is the buy.php script:
[PHP]<html>
<head><title>... test buy ...</title></head>
<body>

<?php

echo "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">";
echo "<tr><td>PRODUCT</td><td>QUANTITY</td></tr>";

while ($count > 1) {
$count--;
$prod = "$"."product"."$count";
echo "prod = $prod <br />";
echo "product.n = $product".$count."<br />";
$quant = "quantity";
$qty = $quant.$count;
echo "<tr><td>$prod</td><td>$qty</td></tr>";
}

echo "</table>";

echo "count is now $count";

?>
</body>
</html>[/PHP]

Now, you can see I am trying to output all the product values and their corresponding quantities in a table, however, instead of actually showing the values, it just shows "$product1" and so on.

See it live at: http://www.cannonballguy.com/stuff/srch.php?keywords=NBC&Submit=Search

Someone suggested using isset() in buy.php but I don't have a clue how.
In fact, any help is greatly appreciated.
I just need to get those values to be displayed in the table on buy.php - but remember there could be 1000's, so I could have $product1 through $product999, which is why I am trying to use $product.$count.

Thanks,
CBG

Comments

  • NuvoNuvo Forum Leader VPS - Virtual Prince of the Server
    [php]$prod = "$"."product"."$count";[/php]
    Will echo out "$product1" and such since that's what you're telling it to do.
    You can't define a variable in a string like that since it would stuff up strings containing variables which shouldn't be parsed when it's echoed.
    If PHP is set up to do it, you can have it automagically create variables for query string values, but many don't have this enabled as it requires Register Globals to be turned on (which isn't always a good idea).

    [php]$prod = $_GET;[/php]
    Would set $prod to the value of product1 in the query string.
    Since you're using a string parameter in the $_GET[], you could try:
    [php]$prod = $_GET;[/php]
    Which would, in theory, set $prod to "product<number>", which you could then use in a WHILE loop like you are at the moment.
    This worked for me in a test script I threw together to test some things while reading your code (I love having a private dev server).

    isset() is rather simple to use.
    Basically, if you do something like:
    [php]if !isset($variable)){
    echo "The variable wasn't set and now we're all doomed!!";
    }[/php]
    It'd only echo the message if the variable has the value of NULL, or nothing (you can use unset() to strip the value of variables if needed).
    The ! is the same as saying "if it doesn't", so it'd be "if the variable has no value", or think of it as the FALSE switch (instead of looking for true results, it looks for false ones).

    Here's something I just put together quickly which cycles through the query string grabbing data from it and putting it on screen as well as checking to see if $var1 has a value:
    [php]
    <?php
    // $variable will hold the number of items in our query string.
    $variable = $_GET;
    // Loop through so long as $variable isn't zero.
    while($variable > 0){
    //Set our variable, which should contain the item I.D. as well as it's individual number hich we're hackishly assuming is the same as the counter value.
    $var1 = $_GET;
    // Print our variable's value to the screen.
    echo $var1.'<br />';
    // Subtract one from our counter variable.
    $variable--;
    }

    // Check if $var1 has a value, isset($var) would also work, but I decided to check if it's unset first because I can.
    if(!isset($var1)){
    // The variable has no value, so echo this:
    echo "Nothing :(";
    // Otherwise, do the following ($var1 has a value)...
    }else{
    // Show this message if the variable has a value:
    echo "Something!!";
    }
    ?>
    [/php]
    That would basically mean that if the URL was page.php?count=2&id1=genetics&id2=Neurology you'd get a page showing:
    Neurology
    Genetics
    Something!!
    While page.php would show "Nothing :(" and page.php?count=2&id=chrome&id2=steel would show "steel" then a blank line and then "Nothing :(" as the variable $var1 would have no value (since "id" has no trailing number, so the script doesn't look for it).

    I know, I'm rambling now, but I hope this gives you some pointers...
    PHP, CSS, XHTML, Delphi, Ruby on Rails & more.
    Current project: CMS Object.
    Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in.
    Release date: NEVER!!!
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Thanks, Nuvo.
    I tried using $prod = $_GET; .
    This is the new code:
    [php]<html>
    <head><title>... test buy ...</title></head>
    <body>

    <?php

    $prod = $_GET;
    $qty = $_GET;
    echo "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">";
    echo "<tr><td>PRODUCT</td><td>QUANTITY</td></tr>";

    while ($count > 1) {
    $count--;
    echo "prod = $prod <br />";
    echo "<tr><td>$prod</td><td>$qty</td></tr>";
    }

    echo "</table>";

    echo "count is now $count";

    ?>
    </body>
    </html>[/php]

    It doesn't output anything now - which I guess is better than listing the variables the themselves, instead of the values.
    See it at the same link...
  • NuvoNuvo Forum Leader VPS - Virtual Prince of the Server
    Simply rectified.
    You need to put the $_GET[]; variables for the product name and quantity inside the loop so that it'll cycle through them.
    Here's a quick mod of your code (works for me...):
    [php]
    <html>
    <head><title>... test buy ...</title></head>
    <body>

    <?php
    // We'll get the count variable without register glabals just in case.
    $count = $_GET;

    echo "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">";
    echo "<tr><td>PRODUCT</td><td>QUANTITY</td></tr>";

    // Start of our loop.
    while ($count > 1) {

    //Subtract one from $count.
    $count--;

    // Get our variable values from the query string.
    $prod = $_GET;
    $qty = $_GET;

    // Print them to HTML so they are readable.
    echo "prod = $prod <br />";

    echo "<tr><td>$prod</td><td>$qty</td></tr>";
    }

    echo "</table>";

    echo "count is now $count";

    ?>
    </body>
    </html>
    [/php]
    PHP, CSS, XHTML, Delphi, Ruby on Rails & more.
    Current project: CMS Object.
    Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in.
    Release date: NEVER!!!
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Yep, that works a treat. Thanks Nuvo.

    Now I need it so that the user has a chance to change the quantities of the products at this stage. Then they can "Save" the products and quantities and continue to search and add more stuff to the 'cart'. So I need to save it to a database.
    Then they also need to enter their personal details, and then hit a button to send all the information in an email.

    This bit I really don't know how to do, so if someone can get me started, I'll be really grateful!

    Thanks,
    CBG
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Umm, *bump*.
    Can someone please help?
    I want to get this done in a day or two.

    Thanks,
    CBG.
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    bump^2
    Really, any help is appreciated.

    Please..? :(
  • AeriffAeriff Junior Member Shared Hoster
    I'll give it a look once I get some time :)
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Okay, this turned into something much too big for my first encounter with MySQL (and still one of my first few encounters with PHP) so I'm now using a cart-script I downloaded. I'm doing my best to edit it to work with my database, to include the search feature and finally to add a customer-details form and have everything sent off in an email...

    Looks like I won't need that help I spammed for now, hopefully.
    Thanks anyway, Aeriff. :)
  • AeriffAeriff Junior Member Shared Hoster
    Mmkay, need any help, just PM me ;)
Sign In or Register to comment.