Show member with id same column

Asked

Viewed 46 times

0

As I do to display a list of members of each post, being that some members are in more than one position. The categories where the members are, are in the same column separated by pipe (|).

Follow the image below:  -

My code only displays the first id (8) for example, in case I click on another post, as for example on id 40, he’s not on the website. Just follow my code:

<?php
$id = isset($_GET['id']) ? $_GET['id'] : '';
$newid = ($id."|");
$sql = mysql_query("SELECT * FROM acp_usuarios u, acp_cargos t WHERE t.id=u.cargos AND u.id=t.user_id AND u.status='Ativo' AND u.ativado='s' AND u.cargos LIKE '$newid' OR u.cargos = '$newid' ORDER BY u.id");
while($ver = mysql_fetch_array($sql)){

?>

2 answers

2

LIKE '%$newid%'

The % indicates in which position you are looking for the result, when you use %$newid% tells us that you are looking for the value of the variable at any position in the column.

Recommended reading: https://www.w3schools.com/sql/sql_like.asp

1


The ideal is to reformulate the database and create a table to associate the position with the user. But if you can’t do this rewrite, you can circumvent this problem as follows:

$id = isset($_GET['id']) ? $_GET['id'] : '';
$sql = mysql_query("SELECT * FROM acp_usuarios u, acp_cargos t WHERE t.id=u.cargos AND u.id=t.user_id AND u.status='Ativo' AND u.ativado='s' AND (u.cargos LIKE '%|$id|%' OR u.cargos LIKE '%|$id' OR u.cargos LIKE '$id|%') ORDER BY u.id");
while($ver = mysql_fetch_array($sql)){

}

The problem of doing so is that you will lose the speed of consultation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.