Jump to content

I want a value not a "Reference #"


dontflinch

Recommended Posts

I am a php/mysql noob, so please forgive me for probably sounding dumb.

 

I'm working on a virtuemart installation and I want to make it so that the only time the "add to cart" form shows is when the category_id=1.  anything else, not there.  obviously category_id is an integer.

 

I've selected the column I want.  $result = mysql_query('SELECT category_id FROM qa_vm_product_category_xref');

 

then I've flailed around for the last 4 hours trying all sorts of stuff like this (and a bunch of other variations):

 

if ($result == '1') { echo $form_addtocart; } else { false ;} 

 

$desired = 1; if (mysql_result(($result,0) == $desired)) { echo $form_addtocart; } else { false ;}

 

$desired = 1; $fieldvalue = mysql_fetch_field($result);  if ($fieldvalue == $desired) { echo $form_addtocart; } else { false ;}

 

since I am so new I cannot even tell if it's just messed up quotes (which I have tried so many ways my eyes are crossed).  I echoed the $result and that's when I saw the Reference # stuff and saw about how mysql_query returns a 'resource' not the value.  other times everything just says 1 (even ones that are category_id 2).

 

any help please? 

rest assured I contribute to the virtuemart and joomla forums occasionally and if ever I get this figured out I will most likely post it there for others that might desire such a fix.  thanks!

 

Link to comment
Share on other sites

trying all sorts of stuff

 

Yes, but did you try reading a php/mysq tutorial so that you would know how to retrieve data from a result resource? You must fetch the data. See any of the mysql_fetch_xxxxxx() instructions in the php.net documentation.

 

And since this is actually a php coding problem, moving thread to the php help forum section...

Link to comment
Share on other sites

yes, thanks for the move.  since it is php calling mysql I figured it was mysql but I guess I was wrong.

 

also in my post I mentioned that I had read the manual and even tried mysql_fetch_field.

 

thanks anyway.

 

edit: well I thought I mentioned, I must have accidentally deleted that part, but if you look at the examples there is a mysql_fetch_field in there.  I did of course go over the manual and have looked through all the mysql "results" stuff but it still isn't making much sense how to pull just one value and compare it to "1".

Link to comment
Share on other sites

mysql_fetch_field just gets field names, and their information.

 

So, this will be a short tutorial.  (very short).

 

//Make your database connection (not included).

//Your query.
$result = mysql_query('SELECT category_id FROM qa_vm_product_category_xref');
//Find out if this query returned rows.
if(mysql_num_rows($result) > 0) {
//If there are rows returned, get them.
while($row = mysql_fetch_assoc($result)) {
  //print the row out.
  echo $row['category_id'] . '<br />';
//close the while loop.
}
//close the if
}
//if there are no rows returned you will see the following.
else {
echo 'No rows returned.';
}

Link to comment
Share on other sites

thank you for replying!

 

your example now has me very close but it is not yet quite right.

 

I have only a small number of products (two items that are category 1 and four items that are category 2 - at least for now) in my vm catalog.  there are only two categories.  the category_id for one is 1 and for the other it is 2. 

 

I only want to show the $form_add-to_cart when the item is of category 1, and not show anything if it is any other category (at this time only one other one).

 

I am always certain there are products within category_id 1.

 

So I have used your code (as follows) and am now getting 112222 printed out on all the products which is the column result.  I think I may need to do some sort of foreach maybe to just return the single cell result for each one?  the right answer eludes me, any chance you might help me out a bit more?

 

I'm including a screenshot of the table, so you can see what I mean about the result being the column as well as the result on the site.

 

$result = mysql_query('SELECT category_id FROM qa_vm_product_category_xref'); while($row = mysql_fetch_assoc($result)) {echo $row['category_id'];} 

 

if I can just get the echo result right I am sure I can figure out the comparison from there.

 

===========================================

 

112222.jpg

 

cat-id-screen.jpg

 

===========================================

 

p.s.  when I try this:

$result = mysql_query('SELECT category_id FROM qa_vm_product_category_xref'); 
$row = (mysql_fetch_assoc($result)); 
foreach ($row as $item) {echo $item['category_id'];} 

I just get a result of 1 on them all.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.