Jump to content

displaying a category name


An7hony

Recommended Posts

I am trying to display a category name related to a 'job' post.

 

The function i create the job with is shown below:

 

<?php
function create_job($params)
{
   db_connect();

	$query =  sprintf("INSERT INTO jobs set 
						jobs.agent = '%s',
		                jobs.branch = '%s',
						jobs.title = '%s',
						jobs.location = '%s',  
						jobs.salary_from = '%s',
						jobs.salary_to = '%s',
						jobs.frequency = '%s',
						jobs.jobtype = '%s',  
                            jobs.description = '%s',
					    jobs.specifications = '%s',
					    jobs.benefits = '%s',
					    jobs.email = '%s',
											  created_at = NOW() 
								", mysql_real_escape_string($params['agent']),
									 mysql_real_escape_string($params['branch']),
								   mysql_real_escape_string($params['title']),
									 mysql_real_escape_string($params['location']),
									 mysql_real_escape_string($params['salary_from']),
									 mysql_real_escape_string($params['salary_to']),
									 mysql_real_escape_string($params['frequency']),
									 mysql_real_escape_string($params['jobtype']),
									 mysql_real_escape_string($params['description']),
									 mysql_real_escape_string($params['specifications']),
									 mysql_real_escape_string($params['benefits']),
									 mysql_real_escape_string($params['email'])

								);

	$result = mysql_query($query);
	if(!$result)
	{
		 return false;
	}

	$job_id = mysql_insert_id();

	if(!empty($params['cats'])) 
	{
       foreach($params['cats'] as $cat_id) 
		 {
			 $query = sprintf("INSERT 
				                     INTO jobs2categories
				                          set
																	 category_id = '%s',
																	 job_id = '%s'",
																	 mysql_real_escape_string($cat_id),
																	 mysql_real_escape_string($job_id)
																	 );

			 $result = mysql_query($query);

			 if(!$result)
			 {
				 return false; 
			 }

		 }

   	}

	 return true;

}	?>

 

Here is the code i use to pull the job data:

 

<?php $query = "SELECT id, agent, branch, title, location, salary_from, salary_to, frequency, jobtype, description, specifications, benefits, email, created_at FROM jobs WHERE branch = '$ulevel' ORDER BY id DESC ";
$result = mysql_query($query) or die('Error : ' . mysql_error());

if(mysql_num_rows($result)==0) {
    echo('
<span><strong>No Jobs from '.$ulevel.' Listed</strong></span>
');
}

while(list($id, $agent, $branch, $title, $location, $salary_from, $salary_to, $frequency, $jobtype, $description, $specifications, $benefits, $email, $created_at) = mysql_fetch_array($result, MYSQL_NUM))
{
?>
      				<tr>
      					
                        <td class="col-first"><?php echo date('d.m.Y',strtotime($created_at));?></td>
      					<td class="col-second">### CATEGORY NAME ###</td>
      					<td class="col-third"><?php echo safe_output($title); ?></td>
                        <td class="col-fourth"><?php echo safe_output($location); ?></td>
                        <td class="col-five"><?php echo safe_output($jobtype); ?></td>
                        <td class="col-sixth"><?php echo safe_output($agent); ?></td>
                        <td class="col-seven">3</td>
      					<td class="row-nav"><a href="#" class="table-edit-link">Edit</a> <span class="hidden"> | </span> <a href="javascript:delArticle('<?php echo safe_output($id); ?>');" class="table-delete-link">Delete</a></td>
      				</tr>
				<?php
}



?>

 

How would i display the related category name in the table above?

 

Any help is appreciated

Link to comment
https://forums.phpfreaks.com/topic/202250-displaying-a-category-name/
Share on other sites

if this is more help here is the form:

<?
<li class="even"><label class="field-title">Categories: </label> <label>
						<?  $categories = find_categories(); ?>
						<?php foreach( $categories as $category): ?>
			<input type="checkbox" name="job[cats][]" value="<?php echo $category['id']; ?>" > <?php echo $category['name']; ?> <br/>
         <?php endforeach; ?></label> 
						<span class="clearFix"> </span></li>
?>

 

and here is the find_categories(); function

<?
function find_categories()
{
   db_connect();

	$query =  "SELECT 
                  categories.name, 
								categories.id,
								COUNT(jobs2categories.job_id) as numjobs
						  FROM
							  categories
						  LEFT JOIN
							   jobs2categories ON categories.id = jobs2categories.category_id
							GROUP BY
							    categories.id
							 ORDER BY
							    categories.name ASC
								";

	$result = mysql_query($query);

	$result = db_result_to_array($result);

	return $result;

}
?>

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.