Jump to content

Creating multidimension array from mysql source and outputting


Recommended Posts

I have whiled my way through any number of forums and help pages so far. However, since I am not quite comfortable yet with PHP, I am not sure which option I have been given would be the right one.

 

What I am trying to do:  Create the source for a set of drop-down menus. The dropdown menu has columns, and then a H2 and P that go in each column, in varied numbers, so you have

column1, h2 p; h2 p; h2 p

column2, h2 p; h2 p

etc....make sense? I am using an MySQL table as source so I can have a little CMS dashboard where I can change these and other values on the fly.

 

So I have a MySQL table where I have a field that identifies the column (page name), and then values to fill the h2 and p tags. I.E.:

page_name, content_title, content_text

 

The idea, therefore, is to use PHP to figure out what the column names (that are repeated) in the mysql table and then create the dropdown based on the value in the subsidiary fields. Output is as a string in a variable value that is output via return ();

 

My best estimation of how to do this is to use php to create a multidimensional array from the query result, where the first column is the page_name, followed by an array, the array being the remainder of the information.

 

Based on information scrounged from other sources, I have come up with this so far:

 

$query = "SELECT * FROM tbl_mainmenu WHERE mainmenu_type = '$mmtype'"; // what the query is
$result = mysql_query ($query, $dbc); // connect to database and run query, setting the the result to dummy array variable '$result'

		// establish the multidimensional array
		$mmcontent_array = array ();

		//create the array where the values are content page and associated values in an array per each content page
		while ($result = mysql_fetch_assoc ())	{
			$page_name = $result['content_page'];
			$mmcontent_array['content_page'] = $result;
									}

		foreach ($mmContent_array[] as $key=>$value)	{

			foreach ()	{

				$tmpelements[] = '<div class="col_2"><h2>' . $element[content_title] . "</h2>\n<p>" . $element[content_text] . "</p>\n</div>";
				$elements = implode("\n",$tmpelements) . "\n"

						}
		$tmp[] = '<li><a href="#" class="drop">Products</a><!-- Begin Products Item -->\n<div class="dropdown_2columns"><!-- Begin 2 columns container -->' . $elements . 'whatever tags will close this')

					}

 

Am I even in the ballpark?

 

I am not sure which way to nest the foreach ()'s. The example I was working from used echo ()'s, and I am not sure if using $variable = implode () makes the task different.

 

As a start, I am just wondering if I am creating the multidimensional array correctly?

This is a project I worked on a couple years ago.  It takes a multi-dim array from the database, and puts it into drop down boxes.  I didn't go through your code, but figured it would either help you, or confuse you more. ;)

 

//call the query
$result = mysql_query($query);

//if we have data to pull, populate it.
if(mysql_affected_rows() > 0)
    {
    
    while($row = mysql_fetch_array($result))
        {
        $join_id = $row[0]; 
        
        //make sure form name's don't have whitespace
        $join_event_id = $row[1]; 
        $join_position = str_replace(' ','_',$row[2]); 
        $join_candidate = $row[3]; 
      
               
        //create a storage array ->Multi-dim.
        $storage[$join_event_id][$join_position][$join_candidate] = $join_id;  
    }

//cycle through our array to build our forms.    
        foreach($storage as $id => $array2)
            { //lets start off by making a form.  There should only be one intitial key, so we will build it on the first loop.
                echo '<form action="" method="post">'
                    .'<input type="hidden" name="election" value="'.$id.'"/>'; //make a hidden input, this will hold the name of our election.
                echo 'Vote in Election: '. $id .'<br/>';
                
                
                        foreach($array2 as $position => $array3)
                            {
                                    echo '<div>'.$position.'<br/>' //this holds the name of our position.
                                        .'<input type="hidden" name="position[]" value="'.$position.'"/>' //this hidden input holds the NAME of our next select input.
                                        .'<select name="'.$position.'">'; //notice that the name of this input is the same as the value of the input above.
                                        
                                
                                    if(is_array($array3))
                                        {
                                        foreach($array3 as $candidate => $canID)
                                            {
                                                echo '<option value="'.$canID.'">'.$candidate.'</option>'; //This is our options.
                                            }
                                        }
                                    else
                                        {
                                            echo '<option value="NULL">Not Set</option>'; //only invoked if there are no candidates (query screwed teh pooch).
                                        }                                
                                    
                                    echo '</select>' //ending our select box.
                                        .'</div>';
                            }
                    
                echo '<br/><input type="submit" name="submit" value="  SUBMIT  "/>'//submit the and end the form.
                    .'</form>';
                    
            }
    }

 

This is how I went about retrieving the values:

//Check to see if the submit button has been hit
if($_POST['submit'])
    {
        //escape our strings to protect from sql injection attacks -- 
        //You could add more checks here
        $election = mysql_real_escape_string($_POST['election']);
        $position = $_POST['position'];
    
    
        //check to see if the user has already voted
        $checkDb = "Query removed.";
            $resultDb = mysql_query($checkDb);
            if(mysql_affected_rows() < 1)
                {
                    foreach($position as $key => $value)
                        {
                            $position = mysql_real_escape_string($value);
                            $id = mysql_real_escape_string($_POST[$value]);
                            $position = str_replace('_',' ',$position);
                            
                            //if user hasn't voted, insert the query, and echo him a thank you.
                            mysql_query("query removed.  I would suggest not to query here, but build multi-values, and insert with 1 query.");
                            
                        }
                    echo 'Thanks for voting!<br/>';
                }
                //if the user has voted, tell him that.
            else echo 'You have already voted!<br/>';
    }

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.