Jump to content

Converting a string into an array?


Perad

Recommended Posts

I am trying to make a function far more useful by allowing more input variables. However i am having some difficulty finishing it off.

 

Here is the original function.

 

public function Find($id = '') {
	if ($id != '') {
		$sql = "SELECT * FROM x_pages WHERE id='$id'";
		$result = mysql_query($sql);				
		$row = mysql_fetch_assoc($result);
		$this->data[] = array('id' => $row['id'], 'nav_image' => $row['nav_image'], 'name' => $row['name'],  'online' => $row['online'], 'order' => $row['p_order']);
	} else {
		$sql = "SELECT * FROM x_pages";
		$result = mysql_query($sql);				
		while ($row = mysql_fetch_assoc($result)) {
			$this->data[] = array('id' => $row['id'], 'nav_image' => $row['nav_image'], 'name' => $row['name'], 'online' => $row['online'], 'order' => $row['p_order']);
		}
	}
}

 

This is the new one so far

 

$value = 'name|online|p_order'

 

public function Find($type, $table, $value, $id = NULL) {
	$values = explode('|', $value);
	for ($i = 0; $i < count($values); $i++) {

	}

	if ($type == 'single') {
		$sql = "SELECT * FROM $table WHERE id='$id'";
		$result = mysql_query($sql);				
		$row = mysql_fetch_assoc($result);
		$this->data[] = $values;
	}
	if ($type == 'all') {
		$sql = "SELECT * FROM $table";
		$result = mysql_query($sql);				
		while ($row = mysql_fetch_assoc($result)) {
			$this->data[] = $values);
		}
	}
}

 

How do I convert the value into the data array I used in my original function?

 

 

Link to comment
Share on other sites

Originally I had this.

 

$this->data[] = array('id' => $row['id'], 'nav_image' => $row['nav_image'], 'name' => $row['name'], 'online' => $row['online'], 'order' => $row['p_order']);

 

This allowed me to call data to by templates by using $function->data['id']['value'];

 

The problem with the function is that it can only do one query and return one set of results. I am trying to allow the function to return any results from any table.

 

My question is. How can I start with this.

 

name|online|p_order

 

And convert it into

 

'name' => $row['name'], 'online' => $row['online'], 'p_order' => $row['p_order'],

Link to comment
Share on other sites

That entire function is pretty convoluted, better simply...

 

<?php

public function find($table, $value, $where = null) {
  if (is_null($where)) {
    $sql = "SELECT $value FROM $table";
  } else {
    $sql = "SELECT $value FROM $table $where";
  }
  if ($result = mysql_query($sql)) {
    if ($num = mysql_num_rows($result)) {
      if ($num > 1) {
        return mysql_fetch_assoc($result);
      } else {
        $tmp = array();
        while ($row = mysql_fetch_assoc($result)) {
          $tmp[] = $row;
        }
        return $tmp;
      }
    }
  }
  return false;
}

?>

 

Even then, this isn't something I would normally bother using.

 

Its a bad idea to select more data then you actually want (SELECT *).

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.