Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to query an array in PHP and return individual rows/items for each element
    primarykey
    data
    text
    <p>Im coding a social web application and Im having some problems. I will ask just one of them as Im unaware of the rules of this forum, i.e, whether I can post more than one problem. I am learning PHP pretty much as I code. I have a table users that has a field called pal_array that holds the user_id's of people who are your pals. Im my example, my pal array has 2 user_id's. </p> <p>I want to be able to query all of a user's pals and be able to show their details e.g thumb pics, names etc. I dont know how to to this properly.</p> <p>My code from pals.php:</p> <pre><code>&lt;?php //Establish the Web Intersect Profile Interaction Token here if(!isset($_SESSION['wipit']))//Check to see if session wipit is set yet { session_register('wipit');//Be sure to register the session if it is not yet set } $thisRandNum = rand(9999999999999,999999999999999999); $_SESSION['wipit'] = base64_encode($thisRandNum); ?&gt; &lt;script type="text/javascript" charset="utf-8"&gt; //jquery functionality for toggling member interaction containers function toggleInteractContainers(x) { if($('#'+x).is(":hidden")) { $('#'+x).slideDown(200); }else { $('#'+x).hide(); } $('.interactContainers').hide(); } // Pal accepting var palRequestURL = "request_as_pal.php"; var thisRandNum = "&lt;?php echo $thisRandNum; ?&gt;"; function acceptPalRequest (x) { $.post(palRequestURL,{ request: "acceptPal", reqID: x, thisWipit: thisRandNum } ,function(data) { $("#req"+x).html(data).show(); }); } function denyPalRequest (x) { $.post(palRequestURL,{ request: "denyPal", reqID: x, thisWipit: thisRandNum } ,function(data) { $("#req"+x).html(data).show(); }); } // End Pal accepting // Pal removal function removeAsPal(a,b) { $("#remove_pal_loader").show(); $.post(palRequestURL,{ request: "removePal", mem1: a, mem2: b, thisWipit: thisRandNum } ,function(data) { $("#remove_friend").html(data).show().fadeOut(12000); }); } // End Pal removal &lt;/script&gt; &lt;?php require_once('Connections/connections.php'); ?&gt; &lt;?php //query username $user_id = $_SESSION['UserSession']; mysql_select_db($database_connections, $connections); $query_user_info = "SELECT username FROM users WHERE user_id='$user_id'"; $user_info = mysql_query($query_user_info, $connections) or die(mysql_error()); $row_user_info = mysql_fetch_assoc($user_info); //code for displaying all your pals $query_pal_array = "SELECT pal_array FROM users WHERE user_id='$user_id'"; $pal_array_result = mysql_query($query_pal_array, $connections) or die(mysql_error()); $row_pal_array = mysql_fetch_assoc($pal_array_result); $pal_array = $row_pal_array['pal_array']; $palList = ""; if($pal_array !="") { $palArray = explode(",",$pal_array); $palCount = count($palArray); $palArray = array_slice($palArray,0,15); $i = 0;//how many times we loop over foreach($palArray as $key =&gt;$array_value) { $i++; //increment by one $palList = "$array_value"; } } else { $palCount = "0"; } ?&gt; &lt;table width="500" border="0"&gt; &lt;tr&gt; &lt;td height="20"&gt;&lt;div class="heading_text_18"&gt;&lt;?php echo $row_user_info ['username']; ?&gt;'s&amp;nbsp;pals &lt;?php echo $palCount ?&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="interactionLinksDiv" align="right" style="border:none;"&gt;&lt;a href="#" onclick="return false" onmousedown="javascript: toggleInteractContainers('pal_requests');"&gt;Pal Requests&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td height="5"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;div class="interactContainers" id="pal_requests"&gt; &lt;?php //container for accepting/rejecting pal requests $pal_requests = "SELECT * FROM pal_requests WHERE mem2='$user_id' ORDER BY pal_request_id ASC LIMIT 50"; $pal_request_query = mysql_query($pal_requests) or die(mysql_error()); $pal_request_num_rows = mysql_num_rows($pal_request_query); if($pal_request_num_rows &lt; 1) { echo '&amp;nbsp;You have no Pal requests at this time.'; exit(); } else { while($row_pal_query = mysql_fetch_array($pal_request_query)) { $request_id = $row_pal_query["pal_request_id"]; $mem1 = $row_pal_query["mem1"]; $query_user = "SELECT user_first_name, user_last_name, picture_thumb_url FROM users LEFT JOIN picture ON users.user_id = picture.user_id AND picture.avatar=1 WHERE users.user_id='$mem1' LIMIT 1"; $user_info = mysql_query($query_user, $connections) or die(mysql_error()); while ($row = mysql_fetch_array($user_info)){ $requesterFirstName = $row["user_first_name"]; $requesterLastName = $row["user_last_name"]; } { if(!empty($row["picture_thumb_url"])) { $avatar = '&lt;a href="user_view.php?user_id2=' . $mem1 . '"&gt;&lt;img src="/NNL/User_Images/' . $row["picture_thumb_url"] . '" width="50" height="50" border="0"/&gt;&lt;/a&gt;'; } else { $avatar = '&lt;a href="user_view.php?user_id2=' . $mem1 . '"&gt;&lt;img src="/NNL/Style/Images/default_avatar.png" width="50" height="50" border="0"/&gt;&lt;/a&gt;'; } echo '&lt;hr /&gt; &lt;table width="100%" cellpadding="5"&gt; &lt;tr&gt; &lt;td width="17%" align="left"&gt;&lt;div style="overflow:hidden; height:50px;"&gt;'. $avatar .'&lt;/div&gt;&lt;/td&gt; &lt;td width="83%"&gt;&lt;a class="ordinary_text_12_blue "href="user_view.php?user_id2=' . $mem1 . '"&gt;'. $requesterFirstName .' '. $requesterLastName .'&lt;/a&gt; wants to be your Pal&lt;br /&gt;&lt;br /&gt; &lt;span id="req' . $request_id . '"&gt; &lt;a class="ordinary_text_12" href="#" onclick="return false" onmousedown="javascript:acceptPalRequest(' . $request_id . ');" &gt;Accept&lt;/a&gt; &amp;nbsp; &amp;nbsp; OR &amp;nbsp; &amp;nbsp; &lt;a class="ordinary_text_12" href="#" onclick="return false" onmousedown="javascript:denyPalRequest(' . $request_id . ');" &gt;Deny&lt;/a&gt; &lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;'; } } } ?&gt; &lt;/div&gt; &lt;?php //get pal avatars $query_pal_info = "SELECT users.user_id, user_first_name, user_last_name, username, picture_thumb_url, avatar FROM users LEFT JOIN picture ON users.user_id = picture.user_id AND picture.avatar=1 WHERE users.user_id = $array_value"; $pal_info = mysql_query($query_pal_info , $connections) or die(mysql_error()); $totalRows_pal_info = mysql_num_rows($pal_info ); echo $totalRows_pal_info; echo "\n&lt;table&gt;"; $i = 5; while ($row_pal_info = mysql_fetch_assoc($pal_info)) { if($i==5) echo "\n\t&lt;tr&gt;"; $thumbnail_user = $row_pal_info['picture_thumb_url'] != '' ? $row_pal_info['picture_thumb_url'] : '../Style/Images/default_avatar.png'; echo "&lt;td width='100' height='100' align='center' valign='middle'&gt;&lt;a href = 'user_view.php?user_id2=$array_value'&gt; &lt;img src='/NNL/User_Images/$thumbnail_user' border='0'/&gt;&lt;/a&gt;&lt;/td&gt;\n"; $i--; if($i==0) { echo "\n\t&lt;/tr&gt;\n\t&lt;tr&gt;"; $i = 5; } } if($i!=5) echo "\n\t\t&lt;td colspan=\"$i\"&gt;&lt;/td&gt;\n\t&lt;/tr&gt;"; echo "\n&lt;/table&gt;"; ?&gt; </code></pre> <p>Near the bottom, I have this query </p> <pre><code>$query_pal_info = "SELECT users.user_id, user_first_name, user_last_name, username, picture_thumb_url, avatar FROM users LEFT JOIN picture ON users.user_id = picture.user_id AND picture.avatar=1 WHERE users.user_id = $array_value"; </code></pre> <p>The variable $array_value holds the array of the user_id's of pals. How will I be able to show individual pals? I also have a question as to why anything below my interactContainers div will not show.</p> <p>Thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload