Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Server-side Scripting _ fetching database info from table ---

Posted by: sanoj96 Mar 23 2019, 09:53 AM

Hello,


So i have seen this been done (while back) but as of now i cant remember how i can get the info from the database.


So basicly i have an database that have the following info:

CODE

bid          userPosted                location      Members               Status               date
1             NameOfUser             lat/lng        1, 2, 3 ,4               new                  DatePosted


The Members table is holding the ids from the users. How can i get that info divided up.
So i can get the ID and then echo out the name of the user ?



Sorry for my bad english and proberly bad explaination... Feel free to ask questions if you guys didnt understand.

Posted by: CharlesEF Mar 23 2019, 10:27 AM

I'm confused. Sounds like you just want to SELECT users and display their Id and Name? Since you've shown no code exactly what do you need help with?

Posted by: sanoj96 Mar 23 2019, 10:46 AM

it is just a quesstion, didnt feel i needed to give the code for this.


Basicly, i have a user table and a group table,

UserID's is is sotred under the row "members" in the Group table. And since i am storing the info like this: (1 , 2 , 3, 4 ) in the members table.

So basicly

the code i have looks like this:

CODE
<?php if($rows["members"] == $users1["id"]){
echo $users1["name"];
}else{ echo "[ERROR: No Match Found in our Database]";
}


as of now it displays "No Match Found in Out Database" or 1,2,3,4

i want it to display

1: John Doe (ID: Name)
2: Jon Doe (ID: Name)
3: Petter Doe (ID: Name)
4: Monica Doe (ID: Name)


Hope this helped!

Posted by: CharlesEF Mar 23 2019, 04:45 PM

$rows["members"] will never equal $users1["id"]. To be honest I would redesign the database. But, assuming you don't want to redesign your database then you need to convert $rows["members"] into an array (see explode command) then use PHP command in_array to check each value in the array against $users1["id"].

Example:

$members = explode(", " , $rows["members"]);
if(in_array($users1["id"], $members))
{
echo $users1["name"];
}
else
{
echo "[ERROR: No Match Found in our Database]";
}

This code is untested but should get you started.

Also, if members doesn't contain comma space ", " then the above code will fail.

Posted by: CharlesEF Mar 23 2019, 05:42 PM

Or, you can forget about the array plan and try this instead. Just use 'strpos' to test for $users1["id"]. Example:

CODE
if(strpos($rows["members"], $users1["id"]) !== false)
{
  echo $users1["name"];
}
else
{
  echo "[ERROR: No Match Found in our Database]";
}

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)