Jump to content

[SOLVED] MYSQL QUERY into PHP ARRAY


TGWSE_GY

Recommended Posts

I am asking this because I am in the pseudo code phase, and I am wanting to know how to process a MYSQL QUERY held in $query into a PHP ARRAY in $ArrayX. I read up on the sort() function however I do not see how this applies to what I am attempting to do.

 

Can anyone help?

 

Thanks!  ;D

Link to comment
https://forums.phpfreaks.com/topic/160548-solved-mysql-query-into-php-array/
Share on other sites

I am populating a list of check boxes in my php script based off of 2 tables in my mysql db 1 holding the users selections and one holding the keys to those numeric values. I then am wanting to populate all the check boxes and have the checkboxes that corrolate to the value held in tbl 1 displayed as checked.

Maybe this will help make more sense here is the code I have so far.......

 

<?php
//Connect to databasee
include('/home/thegayestever/thegayestcommunityever.com/projects/recode/php/includes/configs/db_config.php');

$varCustID = $_SESSION['CustID'];
// Check for Null in entire custID
$queryUserData = mysql_query("SELECT * FROM Profile_Interests WHERE CustID = '$varCustID'");

$queryUserDataResult = $queryUserData;

$queryAnswer = mysql_query("SELECT * FROM answ_interests");
$queryAnswerResult = $queryAnswer;

while ($row = mysql_fetch_array($queryAnswer) or die(mysql_error())) {

	$value = $row[0];
	$txt = $row[1];

	while ($row2 = mysql_fetch_array($queryUserDataResult)) {
		if (mysql_query("SELECT InterestID FROM Profile_Interests WHERE CUSTID = '$varCustID' AND InterestsID = '$value'")) {
			echo "<option type=\"checkbox\" checked=\"checked\" value=\"$value\" name=\"form" . $row[2] "\">$txt";
		} else {
			echo "<option type=\"checkbox\" value=\"$value\" name=\form" . $row[2] "\">$txt";
		}
	}

}
?>

 

 

I am still debugging it but if anyone knows of an easier way please let me know.

Thanks  ;D

Ken no both tables have an InterestsID column, but Profile_Interests has an Interest_Index, CustID, InterestID columns in that order/

 

I got it working here is the code for the working script :

 

 <?php
session_start();
//Connect to databasee
include('/home/thegayestever/thegayestcommunityever.com/projects/recode/php/includes/configs/db_config.php');
// Check for Null in entire custID
//Get the Customers ID #
$varCustID = $_SESSION['CustID'];

//Gets the record that corrisponds
//	$testUserData = mysql_query("SELECT * FROM Profile_Interests WHERE CustID='$varCustID'");
$queryAnswer = mysql_query("SELECT * FROM answ_interests");



while ($row = mysql_fetch_array($queryAnswer)) {

	$answ_value = $row[0];
	$answ_txt = $row[1];
	$answ_formName = $row[2];
	$count = 0;
	$testUserData = mysql_query("SELECT InterestsID FROM Profile_Interests WHERE InterestsID = '$answ_value'");

		if (mysql_num_rows($testUserData) == 1) {
			echo "<input type=\"checkbox\" checked value=\"$answ_value\" name=\"$answ_formName\">$answ_txt";
		} else {
			echo "<input type=\"checkbox\" value=\"$answ_value\" name=\"$answ_formName\">$answ_txt";
		}
}
?>

 

Now my only question is if I have all my check boxes displaying on the page, and the user checks all but one then the one that is not checked will not pass a value to the script thats calling that $_POST variable correct?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.