You're getting the basic idea, but Maxxd's right that you have some issues, and he's given some good advice!
I also note that you're using echo twice and will get duplicate content. You may wish to have the class methods (aka 'functions') return the data instead of echo'ing it.
I also see you're using "SELECT *" in your SQL when you're only returning one field. To avoid that overhead and wasted time, call just the column you're going to return in your query. Also, since you're asking for $row['foo'] (where 'foo' is some string), you're probably going to want fetch_assoc() instead of fetch_array() once you get your result.
Just in case it's tough to wrap your head around it all, here a possible implementation that includes most of his instructions (and mine as well). Sorry about the spacing issues; this board software is fairly new and some of us are having trouble formatting nice clean code with it....
<?php
class segment {
private $db;
private $types;
public function __construct($mysqli, $cid) {
if (is_object($mysqli)) {
$this->db = $mysqli;
}
// allow our method to be called with an integer
$this->types = array(1=>"chem_name", 2=>"physiotherapy_nn", 3=>"commun_gg")
}
public function unitseg_set($param) {
if ($int = intval($param)) {
$param = $this->types[$int];
}
$subject = $this->db->query("SELECT `$param` FROM order_subject WHERE id='" . $cid . "';");
$string = '';
while($row=$subject->fetch_assoc()) {
$string .= "<div>" . $row[$param] . "</div>\n";
}
return $string;
}
}
Your home_segment.php would look like this (unless you really need the data in $account1, $account2, etc. to persist beyond the echo):
<?php
require_once($some_file_with_mysqli_defined_in_it);
require_once($some_file_with_cid_defined_in_it);
require_once('segment.php');
$reports_needed = array(1, 2, 3);
$acc = new segment($mysqli, $cid);
foreach ($reports_needed as $n) {
echo $acc->unitseg_set($n);
}
Hope this helps. 🙂