Jump to content

Recommended Posts

First of, sorry that I keep pestering you guys.

 

Ok, so I'm in a dead end once again. The solution is propably so simple,

that I will be hitting my head to the mousepad afterwards, haha.

 

Ok, so here it goes.

 

I have a value set to a variable in php, so how can I make it check a table

in a MySQL table from, let's say columm ID that if it finds that some value

in the colum ID matches the value set to the variable, it outputs the data

entered to the next colum from the same row it found the id from?

 

The table in question is formed like this:

 

id            img_source

-----------------------------------

1    |      http://site.com/blah1.jpg

2    |      http://site.com/blah2.jpg

3    |      http://site.com/blah3.jpg

4    |      http://site.com/blah4.jpg

 

So if the variable has a value of 3, it checks the database table if it has such a value in the id columm

and then outputs the image link next to it.

 

Thanks again!

Link to comment
https://forums.phpfreaks.com/topic/55221-need-help-once-again/
Share on other sites

Hi,

Check this

<?php

$var=3;

$query="SELECT * FROM table-name WHERE id=$var";//this will display the imagesource for id=3

or

$query="SELECT * FROM table-name WHERE id >$var";//this will display the imagesource greater than id=3

 

$res=mysql_query($query);

 

$row=mysql_fetch_array($res);

echo $row['img_source'];

 

?>

Link to comment
https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-272960
Share on other sites

Oh you'r reply was fast, thank you. I just happened to edit my previous post at the same time, hehe.

If you would be kind enough to look at my previous post for new details.

 

Edit: Indeed that was the issues. It doesn't understand the value the variable has. MySQL error follows. Thanks for the help with the error checking.

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3,5,6,7' at line 1

SELECT * FROM e107_userclass_classes WHERE userclass_id=2,3,5,6,7

Link to comment
https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273177
Share on other sites

Ok, I'll post the code:

 

// ignore these

global $post_info, $user;

 

// this is where i check the users class and this part of the code works, since

// I can output the results with "return $user_class"

 

      if(check_class($post_info['user_class'], TRUE) || check_class($user['user_class'], TRUE))

      {

  $user_class = "$post_info[user_class]";

}

 

 

 

 

$query="SELECT * FROM e107_userclass_classes WHERE userclass_id=$user_class";

 

$res=mysql_query($query) or die(mysql_error()."<br />".$query);

 

$row=mysql_fetch_array($res);

 

return $row['userclass_img'];

 

 

return $user_class;

 

---------------

 

The solution you offered wouldn't work, since the variable is not constant.

I'd want the script to check, let's say a value of 1,8,3 then perhaps store

all those numbers to variables or some other way.

 

So, that it checks if all those numbers (separetly) are found in the id columm

and then outputs the next column from every id the numbers match to.

Link to comment
https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273200
Share on other sites

Thanks for the correction. (Altho that part of the code worked just fine... odd..)

Edit: Actually that gives me a parse error, so that change does nothing good.

Edit2: Oh nvm, fixed it.

 

But still I need help with the aforementioned issue. I know now way on how to do that and it might even be impossible. If only the system I am working on wasn't designed like this...

Link to comment
https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273209
Share on other sites

Ok I'll try. I'l give a bigger picture, which most information ain't really relevant to the script itself, but to get a bigger picture.

 

I have a CMS system and every user have their own user classes (Admins, MOds, etc)

Theese userclasses have an unique id number from 1 to upwards.

Now, the system is designed so that it stores all the userclass' the user has

in the users table with teh format of, ex 2,5,3,6

Then the userclasses themselves are separated in another table as

 

userclass_id        userclass_img                      |  userclass_name

--------------------------------------------------------------------

1                  |    http://blah.com/pic.jpg        |  Admin

2                  |    http://blah.com/pic.jpg        |  Moderator

3                  |    http://blah.com/pic.jpg        |  Whatever

 

 

Ok. So, I have gotten the user's (that has posted something) userclasses to a variable,

but since they are in a format with all the classes in the same columm separated with ,

so I can't compare that value in the variable with userclass_id's

Now what I want to do is get every single number separated from, let's say 2,5,3 (the data in variable)

to new variables or find an alternative way to do this.

 

The goal I'm aiming to is that for every single userclass_id the userclass is formed of, the id's userclass_img columm get's shown on the site (return $blahblah).

 

I hope that helped to understand better what I'm doing.

Otherwise you need to ask some specific questions, since I don't know if I can explain this better.

 

Link to comment
https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273224
Share on other sites

The goal I'm aiming to is that for every single userclass_id the userclass is formed of, the id's userclass_img columm get's shown on the site (return $blahblah).

 

Say you have allready otained a users userclass_id's (eg 2,4,5,8) and it is now held in the $userclasses variable.

 

A simple query such as....

 

SELECT userclass_img FROM nameoftable WHERE userclass_id IN($userclasses);

 

Will get you what you describe.

Link to comment
https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273229
Share on other sites

I know, but since the variable has a value value like 2,3,,3 so it can't match it to the userclass_id

I want the script to separate every number to individual variables

 

Oviously it won't find a id 2,4,6 from the userclass_id columm, since they are only single number's there

Every number that the variable has reperesents one userclass so if the user has more than one, they become separated with coma.

Link to comment
https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273236
Share on other sites

My deepest apologies, I misread.

Yay! It works! *hugs* Thanks so much! Sorry that I tried your patience...

All is well now.... atleast for now.

 

Edit: ACtually there is still one issue, but we are getting there!

It only checks the first number of the $usrclass and outputs the data acording to that, but

id wan't it to go thru all the numbers in the variable. Is that possible?

I mean that if a user has a userclass 1,2 only the picture from the userclass_img for class 1 gets output.

Link to comment
https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273245
Share on other sites

Do you know how to loop through a result? eg;

 

<?php

  $sql = "SELECT userclass_img FROM nameoftable WHERE userclass_id IN($userclasses)";
  if ($result = mysql_query($result)) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) { // loop through the result.
        echo $row['userclass_img']."<br />";
      }
    }
  }

?>

Link to comment
https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273261
Share on other sites

Ok, now my code looks like this:

 

global $post_info, $user;

      if(check_class($post_info['user_class'], TRUE) || check_class($user['user_class'], TRUE))
      {
  		$user_class = $post_info['user_class'];

	}


  $query = "SELECT userclass_img FROM e107_userclass_classes WHERE userclass_id IN($user_class)";

  if ($result = mysql_query($query)) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) { 
          $return $row['userclass_img'];
      }
    }
  }
  

 

 

It still only show's the output for the first number in the variable, not for all, since they are separated with coma. Or atleast I think that's the reason.

 

Btw, Thorpe, you had an error in your syntax.

 

Not:

if ($result = mysql_query($result)) {

 

But:

if ($result = mysql_query($query)) {

 

Edit: Fixed some things...

Link to comment
https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273355
Share on other sites

Since I can't edit my previous post anymore, I'll make a new one.

It seems that if I replace return with echo, I get the whole list, but

it just bloats it randomly to the top of the page and not where it is supposed to be

so return has to be used. I wonder why return doesn't show them all.

Link to comment
https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273385
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.