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
Share on other sites

You mean just getting the result of a query into an array?

 

$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);

//or this if it returns more than 1 row

while($row = mysql_fetch_assoc($result))
{

}

Link to comment
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
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.