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
https://forums.phpfreaks.com/topic/95351-converting-a-string-into-an-array/
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'],

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 *).

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.