Jump to content

Recommended Posts

erm

 

mysql_fetch_array

 

if you want all records then your need to use a loop

 

<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    printf("ID: %s  Name: %s", $row["id"], $row["name"]);
}

mysql_free_result($result);
?> 

Hi, I am trying to convert an ASP function to PHP which creates two linked dropdown boxes.  Here is the code.  The ASP function uses getrows to assign values to the array.  I have used a loop but with no success.  Can you see what I am doing wrong?

 

Thanks, Steve

 

<form action="searchCards.asp" method="post" name="searchForm" id="searchForm" style="margin-bottom: 4; margin-top: 2; margin-right: 2;margin-left: 2;"><div align="center">
  <strong> 
  <? 
		$strSQL = "SELECT x.categoryID, x.categoryName, y.subCategoryID, y.subCategoryName FROM Categories AS x INNER JOIN SubCategories AS y ON x.categoryID = y.categoryID ORDER BY x.categoryName, y.subCategoryName;";
		$query = mysql_query($strSQL); 
		$arrRS = array(); 
            while ($rows = mysql_fetch_array($query)){ 
              $arrRS[] = $rows; 
            } 
		?>
  </strong><strong>Category</strong></span> 
  <?= buildLinkedList("listCategorys",0,1,1,"Select business category","width150","listSubCategorys",2,3,1,"Select business type","width150") ?>
  <?
function buildLinkedList($list1name,$list1value,$list1text,$list1size,$list1prompt,$list1class,$list2name,$list2value,$list2text,$list2size,$list2prompt,$list2class) {
		$strList1 = "<select class='".$list1class."' name='".$list1name."' size='".$list1size."' onchange='getList2(this);'>"."\n";
		$strList1 = $strList1."<option value='' selected='selected'>".$list1prompt."</option>"."\n";
		for ($i=0; $i<=uBound($arrRS,2; $i++) {);
		  if ($arrRS[$list1text,$i] != $strValue) {
		    $strValue = $arrRS[$list1text,$i];
		    $strList1 = $strList1."<option value='".$arrRS[$list1value,$i]."'>";
		    $strList1 = $strList1.$arrRS[$list1text,$i]."</option>"."\n";
		    if (strlen($strAssocArr) > 0) {
			  $strAssocArr = left($strAssocArr, strlen($strAssocArr) - 3)."\n".");"."\n";
		    }
		    $strAssocArr = $strAssocArr.'assocArray["'.$list1name."=".$arrRS[$list1value,$i].'"] = new Array('."\n";
		    $strAssocArr = $strAssocArr.'"","'.$list2prompt.'",'."\n";
	      }
	      $strAssocArrElem = '"'.$arrRS[$list2value,$i].'", "'.$arrRS[$list2text,$i].'",'."\n";
	      $strAssocArr = $strAssocArr.$strAssocArrElem;
        }
        $strList1 = $strList1."</select>"."\n";
		//build the output for the second list
		$strList2 = '<strong>Sub-Category </strong>';
		$strList2 = $strList2."<select class='".$list2class."' name='".$list2name."' size='".$list2size."'>"."\n";
		$strList2 = $strList2."<option value=''> </option>"."\n";
		$strList2 = $strList2."</select>"."\n";
		//build the associative array to populate list 2
		$strAssocArr = mid($strAssocArr, 1, strlen($strAssocArr)-3).");";
		$strArray = "<script type='text/javascript' language='javascript'>"."\n";
		$strArray = $strArray."var assocArray = new Object();"."\n";
		$strArray = $strArray.$strAssocArr."\n";
		//build the javascript functions to operate the listboxes
		$strFunctions = "function getList2(listOptions){"."\n";
		$strFunctions = $strFunctions."  var thisform = listOptions.form;"."\n";
		$strFunctions = $strFunctions."  clearDropDown(thisform.".$list2name.");"."\n";
		$strFunctions = $strFunctions.'  var newvalue = listOptions.name + "=" + listOptions.options[listOptions.selectedIndex].value;'."\n";
		$strFunctions = $strFunctions."  fillDropDown(thisform.".$list2name.", newvalue); // fill the 2nd dropdown options"."\n";
		$strFunctions = $strFunctions."}"."\n";
		$strFunctions = $strFunctions."function clearDropDown(listOptions){"."\n";
		$strFunctions = $strFunctions."  for (var i = listOptions.options.length - 1; i >= 0; i--){"."\n";
		$strFunctions = $strFunctions."    listOptions.options[i] = null;"."\n";
		$strFunctions = $strFunctions."  }"."\n";
		$strFunctions = $strFunctions."  listOptions.selectedIndex = -1;"."\n";
		$strFunctions = $strFunctions."}"."\n";
		$strFunctions = $strFunctions."function fillDropDown(listOptions, vValue){"."\n";
		$strFunctions = $strFunctions.'  if (vValue != "" && assocArray[vValue]){'."\n";
		$strFunctions = $strFunctions."    var arrX = assocArray[vValue];"."\n";
		$strFunctions = $strFunctions."    for (var i = 0; i < arrX.length; i = i + 2){"."\n";
		$strFunctions = $strFunctions."      listOptions.options[listOptions.options.length] = new Option(arrX[i + 1], arrX[i]);"."\n";
		$strFunctions = $strFunctions."    }"."\n";
		$strFunctions = $strFunctions.'  } else listOptions.options[0] = new Option("", "");'."\n";
		$strFunctions = $strFunctions."}"."\n";
		$strFunctions = $strFunctions."</script>"."\n";
		return  $strList1."\n".$strList2."\n".$strArray."\n".$strFunctions."\n";
	  }
	?>
    
  <input type="submit" name="Submit" value="Search">
</form>

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.